Python and the power of 'first class' everything

By ITworld tips  Add a new comment

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:

>>> 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

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question