Doug Hellmann
February, 2011
Friday, February 18, 2011
The Zen of Python
by Tim Peters
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
1 i = 5 + 5 13
2 14 t = ('a', 1, vals, None)
3 s = 'literal string' 15 t[1]
4 16
5 m = """multi-line 17 d = { 'a':'a value',
6 string 18 'b':'b value',
7 """ 19 }
8 20 d['a']
9 us = u'üñîçø∂é string' 21
10 22 if a or b:
11 vals = [ 1, 3, 5, 7, ] 23 do_something()
12 vals[0] 24
• Boolean type
1 t = True
2 f = False
3
4 if t:
5 print 't is true'
$ python booleans.py
6
t is true
7 if not f:
f is false
8 print 'f is false'
True
9
False
10 a = t or f
11 b = t and f
12
13 print a
14 print b
$ python for_loop.py
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
• Garbage collection
• print statement
$ python printing.py
string: string
unicode: üñîçø∂é string
number: 25
list: [1, 'a', None]
dict: {'a': 'a value', 'b': 'b value'}
Friday, February 18, 2011
Printing, with printf-style Formatting
1 # -*- encoding: utf-8 -*-
2
3 my_list = [ 1, 'a', None ]
4 my_dict = { 'a':'a value', 'b':'b value' }
5
6 args = ('string', u'üñîçø∂é string', 5 * 5,
7 my_list, my_dict)
8
9 print '''
10 string : %s
11 unicode: %s
12 number : %d $ python printing_with_format.py
13 list : %s
14 dict : %s string : string
15 ''' % args unicode: üñîçø∂é string
number : 25
list : [1, 'a', None]
dict : {'a': 'a value', 'b': 'b value'}
• Do complex things in a
“Pythonic” way
• Namespace is flat
• socket • asyncore
• select • asynchat
• SocketServer • xmlrpclib
• BaseHTTPServer • SimpleXMLRPCServer
• urllib • smtplib
• httplib • poplib
• ftplib • imaplib
• smtpd • json
• getopt • fileinput
• optparse • cmd
• argparse • readline
• subprocess • multiprocessing
• threading • Queue
• anydbm • shelve
• pickle • sqlite3
• datetime • struct
• grp • ctypes
• pwd • xml
• profile • tarfile
• timeit • csv
• http://pypi.python.org/
• Minimal punctuation
• Built-in documentation
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
1 class MyClass(object):
2 """Class documentation.
3 """
4
5 def __init__(self, arg1, arg2):
6 self.attr1 = arg1
7 self.attr2 = arg2
8
9 def do_something(self):
10 """Does some work and returns a value.
11 """
12 return self.attr1 * self.attr2
13
14 obj = MyClass(1, 2)
15 print obj.do_something()
• Everything is an object
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
$ python string_methods.py
1 print ' string value '.split() ['string', 'value']
2 print ' string value '.strip() string value
3 print 'str' in ' string value ' True
4 print 'CHUGALUG'.startswith('C') True
5 print 'CHUGALUG'.endswith('end') False
6 print ':'.join(['a', 'b', 'c']) a:b:c
1 class MyClass(object):
2
3 def do_something(self, arg1, arg2):
4 return arg1 * arg2
5
6 def my_function(arg1, arg2):
7 return arg1 * arg2
1 class MyClass(object):
2
3 CLASS_ATTR = 'class value'
4
5 def __init__(self, arg):
6 self.attr = arg
7
8 o = MyClass('instance value')
9 print o.attr
10 print o.CLASS_ATTR
$ python dynamic_attribute_lookup.py
instance value
class value
1 class A(object):
2 def do_something(self):
3 print 'in A'
4
5 class B(object): $ python dynamic_typing.py
6 def do_something(self): in A
7 print 'in B' in B
8
9 def work_on_obj(c):
10 c.do_something()
11
12 a = A()
13 b = B()
14
15 work_on_obj(a)
16 work_on_obj(b)
$ python using_regex.py
Found : this
In : Does this text match the pattern?
Range : 5 - 9
Substring: this
• OOP
• Procedural
• Functional
$ python procedural.py
[0, 1, 4, 9, 16]
$ python functional.py
[0, 1, 4, 9, 16]
$ python functional.py
[0, 1, 4, 9, 16]
$ python functional.py
[0, 1, 4, 9, 16]
$ python functional.py
[0, 1, 4, 9, 16]
1 $ python generator_expr.py
2 <generator object <genexpr> at 0x1004d20a0>
3 0
4 1
5 4
6 9
7 16
1 def gen_values(n):
2 for i in range(n):
3 yield i*i
4
5 print gen_values(5)
6
7 for i in gen_values(5):
8 print i
1 $ python generator.py
2 <generator object gen_values at 0x1004d2050>
3 0
4 1
5 4
6 9
7 16
1 def f(n):
2 if n > 0:
3 return n * f(n-1)
4 return 1
5
6 print f('5')
$ python traceback.py
Traceback (most recent call last):
File ".../traceback.py", line 6, in <module>
print f('5')
File ".../traceback.py", line 3, in f
return n * f(n-1)
TypeError: unsupported operand type(s) for -: 'str' and 'int'
1 def f(n):
2 if not isinstance(n, int):
3 raise TypeError('n should be an int')
4 if n > 0:
5 return n * f(n-1)
6 return 1
7
8 print f('5')
$ python raising.py
Traceback (most recent call last):
File ".../raising.py", line 8, in <module>
print f('5')
File ".../raising.py", line 3, in f
raise TypeError('n should be an int')
TypeError: n should be an int
1 def f(n):
2 if not isinstance(n, int):
3 raise TypeError('n should be an int')
4 if n > 0:
5 return n * f(n-1)
6 return 1
7
8 print f('5')
$ python raising.py
Traceback (most recent call last):
File ".../raising.py", line 8, in <module>
print f('5')
File ".../raising.py", line 3, in f
raise TypeError('n should be an int')
TypeError: n should be an int
• Process
• Convert
• Ignore
$ python catching.py
Had an error: n should be an int
Always run
1 print 5 * 1.0
2 print 2 ** 100
3 print 2 * 'abc'
4 print 2 + 'abc'
1 $ python type_coercion.py
2 5.0
3 1267650600228229401496703205376
4 abcabc
5 Traceback (most recent call last):
6 File ".../type_coercion.py", line 4, in <module>
7 print 2 + 'abc'
8 TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 print 5 * 1.0
2 print 2 ** 100
3 print 2 * 'abc'
4 print 2 + 'abc'
1 $ python type_coercion.py
2 5.0
3 1267650600228229401496703205376
4 abcabc
5 Traceback (most recent call last):
6 File ".../type_coercion.py", line 4, in <module>
7 print 2 + 'abc'
8 TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 print 5 * 1.0
2 print 2 ** 100
3 print 2 * 'abc'
4 print 2 + 'abc'
1 $ python type_coercion.py
2 5.0
3 1267650600228229401496703205376
4 abcabc
5 Traceback (most recent call last):
6 File ".../type_coercion.py", line 4, in <module>
7 print 2 + 'abc'
8 TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 print 5 * 1.0
2 print 2 ** 100
3 print 2 * 'abc'
4 print 2 + 'abc'
1 $ python type_coercion.py
2 5.0
3 1267650600228229401496703205376
4 abcabc
5 Traceback (most recent call last):
6 File ".../type_coercion.py", line 4, in <module>
7 print 2 + 'abc'
8 TypeError: unsupported operand type(s) for +: 'int' and 'str'
1 print 5 * 1.0
2 print 2 ** 100
3 print 2 * 'abc'
4 print 2 + 'abc'
1 $ python type_coercion.py
2 5.0
3 1267650600228229401496703205376
4 abcabc
5 Traceback (most recent call last):
6 File ".../type_coercion.py", line 4, in <module>
7 print 2 + 'abc'
8 TypeError: unsupported operand type(s) for +: 'int' and 'str'
• Eliminate redundancy
• Easier to learn
• Easier to remember
1 a = [ 1, 2, 3 ]
2 print a[0]
3
4 a = (1, 2, 3)
5 print a[0] $ python index_api.py
6 1
7 a = 'abc' 1
8 print a[0] a
9 zero value
10 a = {0:'zero value',
11 1:'one value',
12 }
13 print a[0]
• GUI • Web
• GTK • Django
• KDE • AppEngine
• Cocoa • Pyramid
• MS • Flask
Windows
• TurboGears
• Tk
• Pecan
• wxPython
• ctypes
• Jython
• IronPython
• Language moratorium
1 def outer(arg):
2 print 'outer(%s)' % arg
3
4 def inner(val):
5 print 'inner(%s) (arg=%s)' % (val, arg)
6 return val * arg
7
8 return inner
9
10 print 'Creating thrice:'
11 thrice = outer(3)
12
13 print '\nCalling thrice:'
14 print thrice(3)
15 print thrice('a')
1 def outer(arg):
2 print 'outer(%s)' % arg
3
4 def inner(val):
5 print 'inner(%s) (arg=%s)' % (val, arg)
6 return val * arg
7
8 return inner $ python closure.py
9 Creating thrice:
10 print 'Creating thrice:' outer(3)
11 thrice = outer(3)
12 Calling thrice:
13 print '\nCalling thrice:' inner(3) (arg=3)
14 print thrice(3) 9
15 print thrice('a') inner(a) (arg=3)
aaa
• http://docs.python.org/
• http://www.doughellmann.com/
PyMOTW/
• http://us.pycon.org/
3. http://www.flickr.com/photos/grabadonut/539151245/sizes/l/in/photostream/
13. http://www.flickr.com/photos/tobascodagama/3106398602/
17. http://www.flickr.com/photos/benandliz/11065336/
24. http://www.otherlandtoys.co.uk/russian-matryoshka-nesting-dolls-pieces-p-2126.html
32. http://www.dandelionsummers.com/html/building_block.html
36. http://www.flickr.com/photos/photoinference/2494576240/in/faves-40068198@N04/
47. http://www.flickr.com/photos/smithsonian/3112472619/in/faves-40068198@N04/
54. http://www.flickr.com/photos/mherring/3722272868/sizes/l/in/photostream/
57. http://www.webstaurantstore.com/9-3-4-x-4-1-2-unbleached-natural-coffee-filter-12-cup-1000-cs/121CF12U.html
59. http://brainstorm-services.com/wcu-lit/ambiguity.html
66. http://commons.wikimedia.org/wiki/File:Dutch_windmill.jpg
67. http://www.epicpm.org/epic-blogging
68. http://www.skyscanner.net/news/articles/2009/09/002990-travel-tip-10-get-on-last-and-learn-the-art-of-patience.html
69. http://gizmodo.com/5015735/the-top-10-rube-goldberg-machines-featured-on-film
71. http://www.nutricaplabs.com/capsule-manufacturer.aspx
75. http://wmltech.wordpress.com/2006/12/09/got-questions/