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
550
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
350
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
コンテナのトラブルシューティング目線から AWS SAW についてしゃべってみる
kazzpapa3
1
120
元旅行会社の情シス部員が教えるおすすめなre:Inventへの行き方 / What is the most efficient way to re:Invent
naospon
2
180
10分でわかるfreeeのQA
freee
1
3.4k
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
12k
フルカイテン株式会社 採用資料
fullkaiten
0
39k
Microsoft MVPになる前、なってから/Fukuoka_Tech_Women_Community_1_baba
nina01
0
170
「視座」の上げ方が成人発達理論にわかりやすくまとまってた / think_ perspective_hidden_dimensions
shuzon
2
15k
Platform Engineering ことはじめ
oracle4engineer
PRO
8
760
Windows Autopilot Deployment by OSD Guy
tamaiyutaro
0
290
GraphRAGを用いたLLMによるパーソナライズド推薦の生成
naveed92
0
190
Exadata Database Service on Cloud@Customer セキュリティ、ネットワーク、および管理について
oracle4engineer
PRO
0
1.1k
Spring Frameworkの新標準!? ~ RestClientとHTTPインターフェース入門 ~
ogiwarat
2
240
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
31
6.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
700
KATA
mclloyd
29
14k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
505
140k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
The Cult of Friendly URLs
andyhume
78
6k
GraphQLとの向き合い方2022年版
quramy
43
13k
How to Ace a Technical Interview
jacobian
276
23k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Building Your Own Lightsaber
phodgson
102
6.1k
Automating Front-end Workflow
addyosmani
1366
200k
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!