Python and the power of 'first class' everything

By  

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[0](-45)    
45
>>> items[1].sqrt(2)
1.4142135623730951
>>> try:
...     x = math.sqrt(-1)
... except items[2]:
...     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[0],
...            int(raw_fields[1]),
...            float(raw_fields[2]) ]
>>>

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:

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

DevelopmentWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question