Pytfalls

 Pytfalls

Conhecendo e explorando comportamentos não intuitivos do Python

B11eec4cb13d50de922479fcc5e2e803?s=128

Andrews Medina

June 11, 2017
Tweet

Transcript

  1. 2.

    WHOAMI ▸ desenvolvedor na Jusbrasil ▸ contribui com projetos open

    source como Django, Docker, OpenStack, pypy, splinter, tsuru ▸ github.com/andrewsmedina
  2. 6.
  3. 7.
  4. 8.
  5. 10.
  6. 11.
  7. 24.
  8. 25.
  9. 36.

    #1

  10. 37.
  11. 39.

    #2

  12. 43.
  13. 44.
  14. 46.
  15. 47.

    def fib(n): if n <= 2: data = 1 else:

    data = fib(n - 2) + fib(n - 1) return data
  16. 48.

    def fib(n, cache={}): if n not in cache: if n

    <= 2: data = 1 else: data = fib(n - 2) + fib(n - 1) cache[n] = data return cache[n]
  17. 49.

    #3

  18. 57.

    def create_multipliers(): multipliers = [] for i in range(5): def

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  19. 62.

    def create_multipliers(): multipliers = [] for i in range(5): def

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  20. 64.

    #4

  21. 72.

    >>> a = 256 >>> id(a) 4307458704 >>> b =

    256 >>> id(b) 4307458704
  22. 73.
  23. 74.

    #5

  24. 76.

    $ python random.py Traceback (most recent call last): File "random.py",

    line 1, in <module> import random File "random.py", line 3, in <module> print(random.randrange(100)) AttributeError: 'module' object has no attribute 'randrange'
  25. 77.

    #6

  26. 78.
  27. 83.

    #7

  28. 86.

    #8

  29. 87.

    >>> x = [[]] * 5 >>> x [[], [],

    [], [], []] >>> x[0].append(1) ????
  30. 93.

    #9

  31. 105.

    #10

  32. 113.

    >>> dragon = Hero() >>> dragon.spells.append(“fireball”) >>> dragon.spells [“fireball”] >>>

    priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells [“heal”]
  33. 114.
  34. 115.
  35. 116.
  36. 117.
  37. 118.