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
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
210
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
150
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
スクラム成熟度セルフチェックツールを作って得た学びとその活用法
coincheck_recruit
1
140
Lambdaと地方とコミュニティ
miu_crescent
2
370
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
310
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Side Projects
sachag
452
42k
Making Projects Easy
brettharned
115
5.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
The World Runs on Bad Software
bkeepers
PRO
65
11k
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!