Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pytfalls

 Pytfalls

Conhecendo e explorando comportamentos não intuitivos do Python

Andrews Medina

June 11, 2017
Tweet

More Decks by Andrews Medina

Other Decks in Technology

Transcript

  1. WHOAMI ▸ desenvolvedor na Jusbrasil ▸ contribui com projetos open

    source como Django, Docker, OpenStack, pypy, splinter, tsuru ▸ github.com/andrewsmedina
  2. #1

  3. #2

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

    data = fib(n - 2) + fib(n - 1) return data
  5. 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]
  6. #3

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

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  8. def create_multipliers(): multipliers = [] for i in range(5): def

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  9. #4

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

    256 >>> id(b) 4307458704
  11. #5

  12. $ 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'
  13. #6

  14. #7

  15. #8

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

    [], [], []] >>> x[0].append(1) ????
  17. #9

  18. #10

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

    priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells [“heal”]