Download - How does this code work?
Author: Jeff Rush <[email protected]>
Copyright: 2010 Tau Productions Inc.
License: Creative Commons Attribution-ShareAlike 3.0
Date: August 27, 2011
Duration: 50-minutes
Difficulty: intermediate
Keywords: language, techniques, data structures
A slideshow of a series of code snippets illustrating aspects of Python different fromyour usual procedural C code that may not be well-known in the community. Eachcode snippet will be discussed.
assignment (binding not copying)code block (many per source file)
a callablefunctionmethod - bound vs unboundclass, invokes __init__instance, invokes __call__
initializer method: __init__destructor method: __del__allocator method: __new____ribbed__
modulepackage (__init__.py, multi-file, hierarchy)non-package (single .py file)
distribution (grouping of modules)
What Does This Code Do?
Vocabulary Primer
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
1 of 39 09/07/2011 03:45 AM
attribute lookupobj.attrnamegetattr(obj, "attrname")setattr(obj, "attrname", value)
mutable vs immutabletuples vs listsstrings, numbers are immutabledicts are mutablesets vs frozen sets(matters for dict keys)
Vocabulary Primer - Basic Objects
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
2 of 39 09/07/2011 03:45 AM
a sequence vs a mappingorderingkey type (int?)dicts, lists, tuples
a set (keys-only dict)subscripting
element fetchslicing (multi-element)key lookup (aspect of entry)
iterationalways forward, never backcan be brittle if modifying
an iterable (not irritable)operations: get, set, del
Vocabulary Primer - Containers
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
3 of 39 09/07/2011 03:45 AM
Any other terms people are puzzled by?
Vocabulary Primer - Open Discussion
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
4 of 39 09/07/2011 03:45 AM
>>> s = "abcdefgh">>> s[3] = 'x' #FAILS
Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
Strings
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
5 of 39 09/07/2011 03:45 AM
>>> colors = ['red', 'blue', 'green', 'yellow']
>>> result = ''>>> for color in colors:... result += color
>>> result'redbluegreenyellow'
result = ''.join(colors)
More Strings
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
6 of 39 09/07/2011 03:45 AM
What is the output and why it is wrong?
>>> def gimme(length=5):... print length>>>>>> print gimme()
5None
Every callable returns something.
Return Values
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
7 of 39 09/07/2011 03:45 AM
>>> somedict = {'a': 1, 'b': 2}>>> list(somedict) # what do you get?
['a', 'b']
>>> d = { 'person': 'Jack', 'city': 'Conway', 'animal': 'bear' }>>> d = dict(person='Jack', city='Conway', animal='bear')
About Dictionaries
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
8 of 39 09/07/2011 03:45 AM
>>> x = (1, 2, 3)>>> x[0]1
>>> x[0:1]
(1,)
>>> x[99]
Traceback (most recent call last): File "<stdin>", line 1, in <module>IndexError: string index out of range
x[99:]
()
x[0] versus x[0:1]
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
9 of 39 09/07/2011 03:45 AM
>>> x = [1, 2, 3]>>> x.append(4)>>> x[1, 2, 3, 4]
>>> x = [1, 2, 3]>>> x += 4
Raises an exception
>>> x += (4, )>>> x
[1, 2, 3, 4]
list.append versus list += x
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
10 of 39 09/07/2011 03:45 AM
>>> x = 1, 2, 3 # what is x?
>>> x(1, 2, 3)
>>> a, b, c = x>>> [a, b, c] = x>>> (a, b, c) = x
>>> person = (37, ('John', 'Thomas'), 190)>>> a, (b1, b2), c = person
a = 37, b1 = 'John', b2 = 'Thomas', c = 190
Iterable Packing/Unpacking
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
11 of 39 09/07/2011 03:45 AM
In other languages:
temp = aa = bb = temp
In Python:
b, a = a, b
b, c, a = a, b, c
Swapping Values
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
12 of 39 09/07/2011 03:45 AM
"sort on the 2nd and 4th column"
def my_compare(item1, item2):return cmp((item1[1], item1[3]),
(item2[1], item2[3]))
container.sort(cmp=my_compare)
def my_key(item):return (item[1], item[3])
container.sort(key=my_key)
Sorting Against a Complex Key
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
13 of 39 09/07/2011 03:45 AM
Write the following C code into a Pythonic style:
if (a == 1 | a == 3 | a == 7)...
if a in (1, 3, 7):...
constants = frozenset((1, 3, 7))...if a in constants:
...
Testing Against Multiple Values
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
14 of 39 09/07/2011 03:45 AM
if a < b and b < c:... # How many times is b evaluated?
if a() < b() and b() < c():...
Comparison Chaining
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
15 of 39 09/07/2011 03:45 AM
if a < b < c:...
if a() < b() < c():... # Now how many times is b evaluated?
Comparison Chaining cont'd
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
16 of 39 09/07/2011 03:45 AM
if 10 <= x < 20:...
if x == y == z:...
if x is y is z:...
Comparison Chaining cont'd
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
17 of 39 09/07/2011 03:45 AM
>>> x = type(5)(2)>>> x2
>>> x = 5.0...>>> y = type(x)(2.0)>>> y2.0
Making More
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
18 of 39 09/07/2011 03:45 AM
>>> value = True>>> isinstance(value, float)False
>>> isinstance(value, (int, long, float))
True
But why is the above True?
>>> issubclass(bool, int)True
Global Function: isinstance()
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
19 of 39 09/07/2011 03:45 AM
int.__subclasses__()[<type 'bool'>]
basestring.__subclasses__()[<type 'str'
object.__subclasses__()(long output!)
Discovering Subclasses of a Class
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
20 of 39 09/07/2011 03:45 AM
>>> bool(1)True
>>> bool("")False
>>> bool(5.0)True
>>> bool(0.0)False
>>> bool([])False
>>> bool(dict())False
Many Things are Boolean in Nature
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
21 of 39 09/07/2011 03:45 AM
>>> x = ''
>>> not xTrue
>>> x''
>>> not not xFalse
>>> x is TrueFalse
>>> x is FalseFalse
>>> bool(x)False
Kinds of Comparison
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
22 of 39 09/07/2011 03:45 AM
>>> f = False>>> f and 'enabled'False
>>> f= True>>> f and 'enabled''enabled'
"and" / "or"although they are logical and not bitwise operatorsthey do NOT return a boolean!
Short-Circuit Logical Operations
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
23 of 39 09/07/2011 03:45 AM
>>> f = False>>> f and 'enabled' or 'disabled''disabled'
>>> f = True>>> f and 'enabled' or 'disabled''enabled'
>>> statelabel = 'enabled' if globalEnableFlag else 'disabled'
>>> statelabel = ('disabled', 'enabled')[globalEnableFlag]
Short-Circuit Logical Operations (cont'd)
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
24 of 39 09/07/2011 03:45 AM
>>> x = ('xxxx'... 'vvvvv'... 'xxxxx')
>>> x'xxxxvvvvvxxxxx'
>>> x = ('xxxx',... 'vvvvv',... 'xxxxx')
>>> x('xxxx', 'vvvvv', 'xxxxx')
Fun with Literals
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
25 of 39 09/07/2011 03:45 AM
>>> cc_number = "1234-5678-9012-3456">>> ''.join(filter(str.isdigit, cc_number))'1234567890123456'
>>> ''.join(c for c in cc_number if c.isdigit())'1234567890123456'
>>> ss = (1, 2), (6, 7), (8, 3), (1, 8), (12, 8)>>> max(x for x, y in subsampling)12
Where Generators are Useful
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
26 of 39 09/07/2011 03:45 AM
>>> s = "01234567ABCDEFGHabcdefgh">>> [ s[i:i+8] for i in xrange(0, len(s), 8) ]
['01234567', 'ABCDEFGH', 'abcdefgh']
What happens if the length is not a multiple of 8?
Chopping a Sequence into Fixed-Length Pieces
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
27 of 39 09/07/2011 03:45 AM
hash = '308b1f248f914d1483281df98c296e85'sig = ':'.join(map(lambda x: hash[x:x+2], range(0, len(hash), 2)))
sig = ':'.join(hash[x:x+2] for x in range(0, len(hash), 2))
from cStringIO import StringIO
s = StringIO(hash)sig = ':'.join( iter( lambda: s.read(2), '' ) )
More Chopping a Sequence into Pieces
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
28 of 39 09/07/2011 03:45 AM
>>> for x in range(3):... print x012
>>> values = [None] * 3>>> for values[0] in range(3):... for values[1] in range(3):... for values[2] in range(3):... print values
[0, 0, 0][0, 0, 1][0, 0, 2][0, 1, 0]...
About Iteration
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
29 of 39 09/07/2011 03:45 AM
>>> x = "abcdefghijklmnopqrstuvwxyz"
>>> for i in range( len(x) ):... print x[i] # not Pythonic
>>> for c in x:... print x # more Pythonic
>>> for i in range( len(x) ):... print i, x[i]
>>> for i, c in enumerate(x):... print i, x
More About Iteration
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
30 of 39 09/07/2011 03:45 AM
>>> round(2.129128, 3)2.129
>>> round(2.1667000000000001, 3)2.1669999999999998
>>> from decimal import Decimal>>> Decimal('2.1667000000000001').quantize(Decimal('.001'))Decimal('2.167')
Fractional Numbers
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
31 of 39 09/07/2011 03:45 AM
class Container(object):
def __getitem__(self, key):print repr(key)
>>> c = Container()>>> c[3]3>>> c['orange']'orange'
>>> c[3:4]slice(3, 4, None)
>>> c[3:4:3]slice(3, 4, 3)
Object Behavior: Subscripting
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
32 of 39 09/07/2011 03:45 AM
>>> c['apple':'lollipop']slice('apple', 'lollipop', None)
>>> c[:'lollipop']slice(None, 'lollipop', None)
>>> c[:'lollipop':100]slice(None, 'lollipop, 100)
>>> c['beagle':'terrier':'ignorecase']slice('beagle', 'terrier', 'ignorecase')
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
33 of 39 09/07/2011 03:45 AM
>>> c[1, 2](1, 2)
>>> c[(1, 2), (3, 4)]((1, 2), (3, 4))
>>> print [1, 2, 3, 4, 5, 6, 7, 8][::2](1, 3, 5, 7)
>>> print [1, 2, 3, 4, 5, 6, 7, 8][1::2](2, 4, 6, 8)
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
34 of 39 09/07/2011 03:45 AM
>>> x = [1, 2, 3, 4, 5, 6, 7, 8]>>> x[::2] = ['A', 'B', 'C', 'D']>>> x['A', 2, 'B', 4, 'C', 6, 'D', 8]
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
35 of 39 09/07/2011 03:45 AM
class Alpha(object):
def fn(self):if sys.platform == 'linux2':
print xif sys.platform == 'win32':
print x
class Alpha(object):
if sys.platform == 'linux2':def fn(self):
print xif sys.platform == 'win32':
def fn(self):print x
Conditionally Creating Methods
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
36 of 39 09/07/2011 03:45 AM
>>> class Alpha(object):...... for x in range(3):...... def fn(self):... print x...... locals()['fn_%d' % x] = fn
>>> a = Alpha()>>> a.fn_0()0>>> a.fn_1()1>>> a.fn_2()2
Dynamically Creating Methods
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
37 of 39 09/07/2011 03:45 AM
import signalimport pdbimport time
signal.signal(signal.SIGUSR1, lambda x, y: pdb.set_trace())
while 1:print "Hello"time.sleep(5)
# note - on OSX, use signal.SIGINT instead of signal.SIGUSR1
Debugger-On-Demand
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
38 of 39 09/07/2011 03:45 AM
Any Questions?
EAFP It's Easier to Ask Forgiveness than PermissionLBYL Look Before You LeapYAGNI You Ain't Gonna Need ItDRY Don't Repeat Yourself
Discussion
What Does This Code Do? file:///mnt/flash/talk-What_Does_This_Code_Do/talk-Wha..
39 of 39 09/07/2011 03:45 AM