Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pytfalls
Search
Andrews Medina
June 11, 2017
Technology
1
180
Pytfalls
Conhecendo e explorando comportamentos não intuitivos do Python
Andrews Medina
June 11, 2017
Tweet
Share
More Decks by Andrews Medina
See All by Andrews Medina
Organizando dados juŕidicos em grafos
andrewsmedina
0
90
Clean Code - princípios e práticas para um código sustentável
andrewsmedina
0
560
tsuru para quem sabe tsuru
andrewsmedina
0
71
globo.com s2 python
andrewsmedina
5
370
tsuru and docker
andrewsmedina
6
3.5k
pypy - o interpretador mais rapido do velho oeste
andrewsmedina
0
360
fazendo deploys de forma simples e divertida com tsuru
andrewsmedina
3
140
let's go
andrewsmedina
2
300
TDD for Dummies
andrewsmedina
3
370
Other Decks in Technology
See All in Technology
なぜCodeceptJSを選んだか
goataka
0
160
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
110
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
220
Amazon VPC Lattice 最新アップデート紹介 - PrivateLink も似たようなアップデートあったけど違いとは
bigmuramura
0
200
私なりのAIのご紹介 [2024年版]
qt_luigi
1
120
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
220
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
130
社外コミュニティで学び社内に活かす共に学ぶプロジェクトの実践/backlogworld2024
nishiuma
0
260
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
570
2024年にチャレンジしたことを振り返るぞ
mitchan
0
140
Featured
See All Featured
Bash Introduction
62gerente
608
210k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Music & Morning Musume
bryan
46
6.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Facilitating Awesome Meetings
lara
50
6.1k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Agile that works and the tools we love
rasmusluckow
328
21k
Transcript
PYTFALLS
WHOAMI ▸ desenvolvedor na Jusbrasil ▸ contribui com projetos open
source como Django, Docker, OpenStack, pypy, splinter, tsuru ▸ github.com/andrewsmedina
PYTFALLS??!!
A HIDDEN OR UNSUSPECTED DANGER OR DIFFICULTY PITFALL
AN UNEXPECTED PROBLEM OR USUALLY UNPLEASANT SURPRISE GOTCHA
None
None
None
Há muito tempo, numa linguagem muito distante…
> 1 + 1
> 1 + 1 2
> 1 + “1”
> 1 + “1” “11”
> 1 + “1” “11”
> “11” - 1
> “11” - 1
> “11” - 1 10
> “11” - 1 10
> “JS” - 1
> “JS” - 1 NaN
> “JS” - 1 NaN
> Array(16).join("JS" - 2) + " Batman!"
> Array(16).join("JS" - 2) + " Batman!" "NaNNaNNaNNaNNaNNaNNaNNaNNa NNaNNaNNaNNaNNaNNaN Batman!"
None
PYTHON
É UMA LINGUAGEM INTUITIVA PYTHON
ZEN OF PYTHON PYTHON
É LEGÍVEL PYTHON
CRIADA EM 1991 PYTHON
“There should be one-- and preferably only one --obvious way
to do it.”
urllib httplib urllib3
urllib httplib urllib3
“Explicit is better than implicit.”
>>> "Oi" + u" mundo" u'Oi mundo'
>>> "Oi" + u" mundo" u'Oi mundo'
#1
Python 2
USE PYTHON3 TL;DR
#2
def append(element, to=[]): to.append(element) return to
>>> my_list = append(12) >>> print(my_list) ??? >>> my_other_list =
append(42) print(my_other_list) ???
>>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =
append(42) print(my_other_list) ???
>>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =
append(42) print(my_other_list) [12, 42]
>>> my_list = append(12) >>> print(my_list) [12] >>> my_other_list =
append(42) print(my_other_list) [12, 42]
def append(element, to=[]): to.append(element) return to
def append(element, to=None): if to is None: to = []
to.append(element) return to
def fib(n): if n <= 2: data = 1 else:
data = fib(n - 2) + fib(n - 1) return data
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]
#3
def create_multipliers(): return [lambda x : i * x for
i in range(5)]
for multiplier in create_multipliers(): print multiplier(2)
for multiplier in create_multipliers(): print multiplier(2) ? ? ? ?
for multiplier in create_multipliers(): print multiplier(2) ? (0) ? (2)
? (4) ? (6)
for multiplier in create_multipliers(): print multiplier(2) 8 8 8 8
for multiplier in create_multipliers(): print multiplier(2) 8 8 8 8
def create_multipliers(): return [lambda x : i * x for
i in range(5)]
def create_multipliers(): multipliers = [] for i in range(5): def
multiplier(x): return i * x multipliers.append(multiplier) return multipliers
def soma(x, y): return x+y
soma = lambda x, y: x+y
def soma(x, y): return x+y
def create_multipliers(): return [lambda x : i * x for
i in range(5)]
def create_multipliers(): multipliers = [] for i in range(5): def
multiplier(x): return i * x multipliers.append(multiplier) return multipliers
def create_multipliers(): return [lambda x, i=i: i * x for
i in range(5)]
#4
>>> a = 256 >>> b = 256 >>> a
is b True
>>> a = 257 >>> b = 257 >>> a
is b ???
>>> a = 257 >>> b = 257 >>> a
is b False
>>> a = 257 >>> b = 257 >>> a
is b False
>>> a = 256 >>> b = 256 >>> a
is b True
>>> help(id)
>>> help(id) id(obj, /) Return the identity of an object.
>>> a = 256 >>> id(a) 4307458704 >>> b =
256 >>> id(b) 4307458704
>>> a = 257 >>> id(257) 4312868592 >>> b =
257 >>> id(b) 4311130416
#5
# random.py import random random.randrange(100)
$ 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'
#6
*.pyc
$ find . -name "*.pyc" -delete
export PYTHONDONTWRITEBYTECODE=1
>>> import sys >>> sys.dont_write_bytecode True
$ python -B main.py
#7
>>> linguagens = {} >>> linguagens[1] = “Python” >>> linguagens[1.0]
= “Go” >>> linguagens[1] “Go”
>>> hash(1) == hash(1.0) True
#8
>>> x = [[]] * 5 >>> x [[], [],
[], [], []] >>> x[0].append(1) ????
>>> x[0].append(1) ????
>>> x[0].append(1) # [[0], [], [], [], []] ok?
>>> x[0].append(1) [[1], [1], [1], [1], [1]]
>>> x[0].append(1) [[1], [1], [1], [1], [1]]
>>> id(x[0]) 4390176008 >>> id(x[1]) 4390176008
#9
class Hero: life = 100 class Dragon(Hero): pass class Giant(Hero):
pass
>>> Hero.life, Giant.life, Dragon.life 100, 100, 100
>>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life
>>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life ?, ?,
?
>>> Hero.life = 10 >>> Hero.life, Giant.life, Dragon.life 10, 10,
10
>>> Giant.life = 300 >>> Hero.life, Giant.life, Dragon.life ?, ?,
?
>>> Giant.life = 300 >>> Hero.life, Giant.life, Dragon.life 10, 300,
10
>>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life ?, ?,
?
>>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life 50, 300,
50
>>> Hero.life = 50 >>> Hero.life, Giant.life, Dragon.life 50, 300,
50
Hero (life=50) Giant (life=300) Dragon
#10
class Hero: spells = []
>>> dragon = Hero() >>> dragon.spells.append(“fireball”) >>> dragon.spells [“fireball”]
>>> priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells ???
>>> priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells [“fireball”, “heal”]
>>> priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells [“fireball”, “heal”]
class Hero: spells = []
class Hero: def __init__(self): self.spells = []
>>> dragon = Hero() >>> dragon.spells.append(“fireball”) >>> dragon.spells [“fireball”] >>>
priest = Hero() >>> priest.spells.append(“heal”) >>> priest.spells [“heal”]
#DICAS
None
None
None
OBRIGADO!