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
ハイテク休憩
sat
PRO
2
140
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
410
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
240
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
530
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
150
podman_update_2024-12
orimanabu
1
270
非機能品質を作り込むための実践アーキテクチャ
knih
3
1.1k
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
170
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
2
260
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
2
2.4k
LINE Developersプロダクト(LIFF/LINE Login)におけるフロントエンド開発
lycorptech_jp
PRO
0
120
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Unsuck your backbone
ammeep
669
57k
It's Worth the Effort
3n
183
28k
The Invisible Side of Design
smashingmag
298
50k
Raft: Consensus for Rubyists
vanstee
137
6.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Mobile First: as difficult as doing things right
swwweet
222
9k
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!