Andrews Medina
June 11, 2017
180

# Pytfalls

Conhecendo e explorando comportamentos não intuitivos do Python

June 11, 2017

## Transcript

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

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

to do it.”

37. ### >>> my_list = append(12) >>> print(my_list) ??? >>> my_other_list =

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

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

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

append(42) print(my_other_list) [12, 42]

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

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

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

i in range(5)]

? (4) ? (6)

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

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

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

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

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

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

i in range(5)]

is b True

is b ???

is b False

is b False

is b True

68. ### >>> a = 256 >>> id(a) 4307458704 >>> b =

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

257 >>> id(b) 4311130416

72. ### \$ 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'

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

= “Go” >>> linguagens[1] “Go”

83. ### >>> x = [[]] * 5 >>> x [[], [],

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

pass

?

10

?

10

?

50

50

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

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