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
360
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
360
Other Decks in Technology
See All in Technology
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
1.3k
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
180
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
330
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
760
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
150
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Unsuck your backbone
ammeep
668
57k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Speed Design
sergeychernyshev
25
620
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Writing Fast Ruby
sferik
627
61k
Designing the Hi-DPI Web
ddemaree
280
34k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Documentation Writing (for coders)
carmenintech
65
4.4k
Making Projects Easy
brettharned
115
5.9k
GitHub's CSS Performance
jonrohan
1030
460k
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!