by David Beazley -- Most Python programmers quickly appreciate the extreme flexibilty provided by its built-in list and dictionary datatypes. One such feature is the fact that these containers can hold any kind of data whatsoever. For example, a list can contain unrelated types of data like this:
>>> s = ['ACME', 100, 123.45]
In this case, 's' is a list containing a string, an integer, and float. This is quite different from how arrays work in many other programming languages. For instance, if you declare an array in C, the elements all have to be the same type.
When working with containers such as lists and dictionaries, you tend to think in terms of simple data structures. For example, you might have a list of names, and a list of numbers, a list of tuples, or a list of instances. However, an often overlooked aspect of Python is the fact that everything that can be named can also be treated as data -- that is, everything is said to have "first class status." This includes functions, classes, methods, modules, and exceptions. For example, consider this list:
>>> import math >>> items = [abs, math, ValueError ]
Here, 'items' is a list containing a built-in function abs(), the math module, and the ValueError exception. Here are some examples of using these items through list lookups:
>>> items(-45) 45 >>> items.sqrt(2) 1.4142135623730951 >>> try: ... x = math.sqrt(-1) ... except items: ... print "Didn't work" ... Didn't work >>>
At first glance, it might not be obvious why you would ever want to do something like this except as a possible way to make your code more difficult to understand (maybe in an attempt to increase your job security). However, knowing about this part of Python can also be used to write extremely compact and flexible code.
For example, suppose you were reading lines of text from a CSV file that looked like "ACME,100,123.45". Now, suppose you wanted to convert the line into a list of fields with appropriate type-conversion applied to each column. One way to do it would be to simply hardcode the conversion for each column like this:
>>> line = "ACME,100,123.45" >>> raw_fields = line.split(',') >>> fields = [ raw_fields, ... int(raw_fields), ... float(raw_fields) ] >>>
However, another way to do this is to simply define a list of types (which are first-class objects) and execute a few simple list processing operations:
>>> field_types = [str, int, float] >>> fields = [ty(val) for ty,val in zip(field_types,raw_fields)] >>> fields ['ACME', 100, 123.45] >>>
In this second example, if you want to convert data from a different file with different columns, you just make changes to field_types. All of the other code works without modification.
|This tip is written by David Beazley, author of the Python Essential Reference, now available in a new Fourth Edition. It is published by Addison-Wesley Professional, as part of its Developer's Library (informit.com/devlibrary), Copyright 2009 by Pearson Education, Inc. ISBN 0672329786. To read a sample chapter, on Types and Objects, please visit the publisher site: http://www.informit.com/title/0672329786 or, Safari Books Online subscribers can access the entire book here: http://safari.informit.com/9780768687040|