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

Pytfalls

 Pytfalls

Conhecendo e explorando comportamentos não intuitivos do Python

B11eec4cb13d50de922479fcc5e2e803?s=128

Andrews Medina

June 11, 2017
Tweet

Transcript

  1. PYTFALLS

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

    source como Django, Docker, OpenStack, pypy, splinter, tsuru ▸ github.com/andrewsmedina
  3. PYTFALLS??!!

  4. A HIDDEN OR UNSUSPECTED DANGER OR DIFFICULTY PITFALL

  5. AN UNEXPECTED PROBLEM OR USUALLY UNPLEASANT SURPRISE GOTCHA

  6. None
  7. None
  8. None
  9. Há muito tempo, numa linguagem muito distante…

  10. > 1 + 1

  11. > 1 + 1 2

  12. > 1 + “1”

  13. > 1 + “1” “11”

  14. > 1 + “1” “11”

  15. > “11” - 1

  16. > “11” - 1

  17. > “11” - 1 10

  18. > “11” - 1 10

  19. > “JS” - 1

  20. > “JS” - 1 NaN

  21. > “JS” - 1 NaN

  22. > Array(16).join("JS" - 2) + " Batman!"

  23. > Array(16).join("JS" - 2) + " Batman!" "NaNNaNNaNNaNNaNNaNNaNNaNNa NNaNNaNNaNNaNNaNNaN Batman!"

  24. None
  25. PYTHON

  26. É UMA LINGUAGEM INTUITIVA PYTHON

  27. ZEN OF PYTHON PYTHON

  28. É LEGÍVEL PYTHON

  29. CRIADA EM 1991 PYTHON

  30. “There should be one-- and preferably only one --obvious way

    to do it.”
  31. urllib httplib urllib3

  32. urllib httplib urllib3

  33. “Explicit is better than implicit.”

  34. >>> "Oi" + u" mundo" u'Oi mundo'

  35. >>> "Oi" + u" mundo" u'Oi mundo'

  36. #1

  37. Python 2

  38. USE PYTHON3 TL;DR

  39. #2

  40. def append(element, to=[]): to.append(element) return to

  41. >>> my_list = append(12) >>> print(my_list) ??? >>> my_other_list =

    append(42) print(my_other_list) ???
  42. >>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =

    append(42) print(my_other_list) ???
  43. >>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =

    append(42) print(my_other_list) [12, 42]
  44. >>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =

    append(42) print(my_other_list) [12, 42]
  45. def append(element, to=[]): to.append(element) return to

  46. def append(element, to=None): if to is None: to = []

    to.append(element) return to
  47. def fib(n): if n <= 2: data = 1 else:

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

  50. def create_multipliers(): return [lambda x : i * x for

    i in range(5)]
  51. for multiplier in create_multipliers(): print multiplier(2)

  52. for multiplier in create_multipliers(): print multiplier(2) ? ? ? ?

  53. for multiplier in create_multipliers(): print multiplier(2) ? (0) ? (2)

    ? (4) ? (6)
  54. for multiplier in create_multipliers(): print multiplier(2) 8 8 8 8

  55. for multiplier in create_multipliers(): print multiplier(2) 8 8 8 8

  56. def create_multipliers(): return [lambda x : i * x for

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

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  58. def soma(x, y): return x+y

  59. soma = lambda x, y: x+y

  60. def soma(x, y): return x+y

  61. def create_multipliers(): return [lambda x : i * x for

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

    multiplier(x): return i * x multipliers.append(multiplier) return multipliers
  63. def create_multipliers(): return [lambda x, i=i: i * x for

    i in range(5)]
  64. #4

  65. >>> a = 256 >>> b = 256 >>> a

    is b True
  66. >>> a = 257 >>> b = 257 >>> a

    is b ???
  67. >>> a = 257 >>> b = 257 >>> a

    is b False
  68. >>> a = 257 >>> b = 257 >>> a

    is b False
  69. >>> a = 256 >>> b = 256 >>> a

    is b True
  70. >>> help(id)

  71. >>> help(id) id(obj, /) Return the identity of an object.

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

    256 >>> id(b) 4307458704
  73. >>> a = 257 >>> id(257) 4312868592 >>> b =

    257 >>> id(b) 4311130416
  74. #5

  75. # random.py import random random.randrange(100)

  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'
  77. #6

  78. *.pyc

  79. $ find . -name "*.pyc" -delete

  80. export PYTHONDONTWRITEBYTECODE=1

  81. >>> import sys >>> sys.dont_write_bytecode True

  82. $ python -B main.py

  83. #7

  84. >>> linguagens = {} >>> linguagens[1] = “Python” >>> linguagens[1.0]

    = “Go” >>> linguagens[1] “Go”
  85. >>> hash(1) == hash(1.0) True

  86. #8

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

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

  89. >>> x[0].append(1) # [[0], [], [], [], []] ok?

  90. >>> x[0].append(1) [[1], [1], [1], [1], [1]]

  91. >>> x[0].append(1) [[1], [1], [1], [1], [1]]

  92. >>> id(x[0]) 4390176008 >>> id(x[1]) 4390176008

  93. #9

  94. class Hero: life = 100 class Dragon(Hero): pass class Giant(Hero):

    pass
  95. >>> Hero.life, Giant.life, Dragon.life 100, 100, 100

  96. >>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life

  97. >>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life ?, ?,

    ?
  98. >>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life 10, 10,

    10
  99. >>> Giant.life = 300 >>> Hero.life, Giant.life, Dragon.life ?, ?,

    ?
  100. >>> Giant.life = 300 >>> Hero.life, Giant.life, Dragon.life 10, 300,

    10
  101. >>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life ?, ?,

    ?
  102. >>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life 50, 300,

    50
  103. >>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life 50, 300,

    50
  104. Hero (life=50) Giant (life=300) Dragon

  105. #10

  106. class Hero: spells = []

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

  108. >>> priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells ???

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

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

  111. class Hero: spells = []

  112. class Hero: def __init__(self): self.spells = []

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

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

  115. None
  116. None
  117. None
  118. OBRIGADO!