Documente Academic
Documente Profesional
Documente Cultură
-eath to N+0
Effective Python Programming OSCON 2005
Effective 12 Efficient
Python 2(5
CPython
Effective Python Programming OSCON 2005
Python 6#ndamenta"
)earning by tin7ering
Everything$" an object
Name",ace"
E86P
-#c7 3y,ing
Effective Python Programming OSCON 2005
Short 8ttention S,an 3heatre
+ntro",ection
E=,erimentation
Effective Python Programming OSCON 2005
+nteractive Python
bonanza% python
Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Deban 1:3.3.5!"#b#nt#2)$ on %n#&2
'ype (he%p(, ()opyr*ht(, ()re+t,( or (%)en,e( ...
---
Effective Python Programming OSCON 2005
he, and dir
Stored in a name",ace
>defa#t" to the oca name",ace?
Effective Python Programming OSCON 2005
Name",ace"(((
oca"
mod#e goba"
b#it.in"
Effective Python Programming OSCON 2005
8""ignment
Permi""ion%
0 ha,attr(ob5, 27a%#e2):
7a%#e 1 ob5.7a%#e
e%,e:
7a%#e 1 Cone
6orgivene""%
try:
rea+ 1 ob5.7a%#e
e&)ept 8ttrb#teDrror:
7a%#e 1 Cone
Effective Python Programming OSCON 2005
E86P
6ie.i7e object"
*o,e(interface
PyProtoco"
-oc#mentation
8da,tation
6#t#re Python
Effective Python Programming OSCON 2005
Str#ct#red Programming
Effective Python Programming OSCON 2005
Contro 6o/
+terator"
Generator"
forBe"e
tryBfinay
tryBe=ce,tBe"e
"/itch "tatement
Effective Python Programming OSCON 2005
S(8(S( 3heatre1
en#merate
0or n n ran*e(%en(,eE#en)e)):
e%e.ent 1 ,eE#en)e[n$
in"tead%
0or n, e%e.ent n en#.erate(,eE#en)e):
/hie
for
tryBe=ce,t
Effective Python Programming OSCON 2005
+terator"
Dore fe=ibe
Effective Python Programming OSCON 2005
6ie" are iterator"
:et#rn" a ine
--- 0or %ne n open(24et)4re,o%7.)on02):
... prnt (*ot %ne 2%,2(%(%ne.,trp())
...
*ot %ne 2na.e,er7er 210.15.254.2402
*ot %ne 2na.e,er7er 210.15.254.2412
*ot %ne 2na.e,er7er 203.10.110.1012
*ot %ne 2na.e,er7er 203.1G.103.12
Effective Python Programming OSCON 2005
Creating iterator"
iter>? b#it.in
Sibing to P#"h+terator
f#nctiona ,rogramming
Effective Python Programming OSCON 2005
Generator"
or $ret#rn$
-B.8P+
!"ing thi"%
0or re, n Me,#%t<ter()#r,or):
pro)e,,Mo9(re,)
Effective Python Programming OSCON 2005
forBe"e
re"o#rce cean#,
%o)I 1 a)E#reNo)I()
try:
7a% 1 +oOo.eOt#00()
0 7a% , Cone:
ra,e Pa%#eDrror(2*ot Cone2)
e%0 7a% F 0:
ret#rn
0na%%y:
%o)I.re%ea,e()
Effective Python Programming OSCON 2005
tryBfinay
Chance" are it /i
Program -efen"ivey
3he !niver"e 0ate" Ho#(
4oef#y #nder#"ed
Effective Python Programming OSCON 2005
tryBe=ce,tBe"e
ifBeifBeifBeifBe"e
#"e a dictionary
Effective Python Programming OSCON 2005
-i",atch via dict
0 n+ata 11 2>3QQ<D2:
,ho9>ozze()
e%0 n+ata 11 2RDMM<'2:
,ho9Rer.t()
...
e%,e:
,ho9BnIno9nM#ppet()
become"%
)a%%D)t 1 @ 2>3QQ<D2: ,ho9>ozze,
2RDMM<'2: ,ho9Rer.t,
... A
0#n) 1 )a%%D)t.*et(n+ata, ,ho9BnIno9nM#ppet)
0#n)()
Effective Python Programming OSCON 2005
Object Oriented Programming
Effective Python Programming OSCON 2005
!"ing Ca""e" Pythonicay
Dore -#c7"1
i"in"tance
inheritance, mi=in"
acce"" contro
C&&B'ava.i"m
non.determini"tic
Or #"e an +nterface
Effective Python Programming OSCON 2005
+nterface"
-oc#mentation
8""ertion"
+e0 *et8#+o>ro.Oo#r)e(ob5):
0 not <8#+o(ob5):
ra,e Pa%#eDrror(2e&pe)te+ a#+o, *ot %r2%
ob5)
ret#rn a#+o.rea+()
8da,tation
8#tomaticay ada,t an +6oo to an +Bar
Effective Python Programming OSCON 2005
+nterface E=am,e
)%a,, <8#+o(<nter0a)e):
222No9%e7e% nter0a)e to a#+o ,o#r)e4,nI.222
+e0 )%o,e():
222C%o,e the #n+er%yn* a#+o +e7)e222
+e0 reopen():
222Meopen a )%o,e+ a#+o +e7)e222
+e0 ,3pen():
222Met#rn 'r#e 0 #n+er%yn* a#+o open222
+e0 rea+():
222Met#rn a pa)Iet o0 a#+o 0ro. +e7)e.222
+e0 9rte(+ata):
222;rte a#+o to +e7)e.222
Effective Python Programming OSCON 2005
!"ing an +nterface
)%a,, 8NO88#+o(ob5e)t):
.p%e.ent,(<8#+o)
+e0 reopen(,e%0):
....
+e0 )%o,e(,e%0):
....
+e0 ,3pen(,e%0):
....
a%,a 1 8NO88#+o()
<8#+o..p%e.ente+Jy(a%,a)
<8#+o(a%,a)
Effective Python Programming OSCON 2005
+nterface" are dynamic
8""ert that a I
rd
,arty object
im,ement" an interface
Brea7" "#bca""ing
!"e i"in"tance%
0 ,n,tan)e(n#., ba,e,trn*):
n#. 1 nt(n#.)
0 not ,n,tan)e(thn*, (nt, 0%oat)):
ra,e Pa%#eDrror(2e&pe)te+ a n#.ber2)
Effective Python Programming OSCON 2005
inheritance, mi=in"
8nother C&&B'ava.i"m
friend, ,#bic, ,rivate, (((
+n ,ractice, annoying
Per"ona goa for 2(5 i" to remove a #"e
from the "tdib
Effective Python Programming OSCON 2005
Sim,ifying yo#r 8P+"
6ir"t attem,t%
0or )h%+ n )ontaner.*etCh%+ren():
+oOo.ethn*;th3b5e)t()h%+)
!nnece""ary 8P+
E=am,e"%
??,tr?? ! ,trn* repre,entaton
??,ette.?? !
ob5[Iey$ 1 7a% 22K ob5.??,ette.??(Iey,7a%)
??a++?? ! a++ ,o.ethn* to th, ob5e)t
??*etattr?? ! *et an attrb#te o0 th, ob5e)t
??eE?? ! ob5e)t , ben* )o.pare+ to another
Effective Python Programming OSCON 2005
",ecia method", e=am,e"
8 & B
6ir"t trie" 8(EEaddEE>B?
3hen B(EEraddEE>8?
>4e$re ignoring EEcoerceEE?
8 22 B
+n order% 8(EEeCEE>B?, B(EEeCEE>8?,
8(EEcm,EE>B?, and then B(EEcm,EE>8?
Effective Python Programming OSCON 2005
Da7e the 8P+ +nt#itive
Pain, other/i"e
in"tead of%
prnt ,o.eob5e)t.*etPa%#e()
,o.eob5e)t.,etPa%#e(5)
#"e%
prnt ,o.eob5e)t.7a%#e
,o.eob5e)t.7a%#e 1 5
Effective Python Programming OSCON 2005
GetBSet method"
!"e a ,ro,erty1
)%a,, Ma*):
+e0 ?*etMa*)C#.(,e%0):
ret#rn 0n+'heMa*)C#.ber()
+e0 ?,etMa*)C#.(,e%0, 7a%#e)
,et'heMa*)C#.ber(7a%#e)
.a*)C#.ber 1 property(?*etMa*)C#.,
?,etMa*)C#.)
Effective Python Programming OSCON 2005
Pro,ertie"
8ternate con"tr#ctor"
)%a,, '.eOta.p(ob5e)t):
+e0 ??nt??(,e%0, h, ., ,):
,e%0.h, ,e%0.., ,e%0., 1 h, ., ,
W)%a,,.etho+
+e0 0ro.Otrn*()%,, ,trn*):
h,.,, 1 [nt(&) 0or & n ,trn*.,p%t(2:2)$
ret#rn )%,(h, ., ,)
-ecorator"
:e,ace" thi"%
+e0 a%ternateCtor()%,, ar*#.entN,t):
...
a%ternateCtor 1 )%a,,.etho+(a%ternateCtor)
4ith thi"%
W)%a,,.etho+
+e0 a%ternateCtor()%,, ar*#.entN,t):
...
Effective Python Programming OSCON 2005
-ecorator"
dict("etdefa#t>7ey, defa#tva?
3#rn" thi"%
0 Iey n +)tob5:
+)tob5[Iey$.appen+(7a%)
e%,e:
+)tob5[Iey$ 1 [7a%$
+nto thi"%
+)tob5.,et+e0a#%t(Iey, [$).appen+(7a%)
Effective Python Programming OSCON 2005
SeC#ence !n,ac7ing
8nnoying
-eb#gging
3e"ting
Effective Python Programming OSCON 2005
S(8(S( 3heatre
Port of '#nit
Dore Pythonic
tra)eba)I mod#e
try:
....
e&)ept:
e, 7, tb 1 ,y,.e&)?n0o()
tra)eba)I.prnt?tb(t)
Effective Python Programming OSCON 2005
cgitb
3hing" to avoid
-on$t
Dod#e 8%
.port .o+#%eJ
Dod#e B%
.port .o+#%e8
Even "im,er
--- 0ro. ,trn* .port 'e.p%ate
--- , 1 'e.p%ate(2\)hee,e , 0ro. \@)o#ntryA2)
--- prnt ,.,#b,tt#te()hee,e12Go#+a2, )o#ntry12the
Cether%an+,2)
Go#+a , 0ro. the Cether%an+,
--- prnt ,.,#b,tt#te()hee,e12Go#+a2)
'ra)eba)I (.o,t re)ent )a%% %a,t):
[...$
ReyDrror: 2)o#ntry2
--- prnt ,.,a0e?,#b,tt#te()hee,e12Go#+a2)
Go#+a , 0ro. \@)o#ntryA
Effective Python Programming OSCON 2005
P 22 Per1A1
O>N?
Some of it i" od
So /ra, in > ?
7ey in dict
dict(get>7ey, defa#t?
Effective Python Programming OSCON 2005
Circ#ar reference"
O,timi"ation tric7
Either%
Node(chidNode"
or
Da7e Node an iterator that iterate"
thro#gh it$" chidren
Effective Python Programming OSCON 2005
get@a#e>?B"et@a#e>?
:eadabiity co#nt"(
3here "ho#d be one and ,referaby ony one obvio#" /ay to do it(
8tho#gh that /ay may not be obvio#" at fir"t #ne"" yo#$re -#tch(