RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Power Python: Do More with Less Code

Python offers some elegant constructs that enable you to write clearer, more concise code. Use these powerful language features to do more with less code.

hat's Power Python, you ask? It's the effective use of Python language features to get a lot of work done in fewer lines of code. The lambda, reduce, filter, map, and list comprehension constructs are some of the features that best fit this definition. This article examines each one in turn and highlights some of their strengths with simple code examples that illustrate the basic syntax and usage. It then delves into some related coding advantages that Python offers, such as the easy creation of callbacks and closures.

The built-in Python functions discussed adhere to some of the basic principles of Functional Programming (FP). While this article doesn't focus on the philosophy of FP per se, it does highlight some of the advantages of the FP style of programming, such as the emphasis on list processing, the use of functions to replace looping, the avoidance of side effects, and the reliance on functions being first class objects. By the end of the article, you should have a strong grasp of how you can use these powerful techniques to write more expressive and concise code in your projects.

Author's Note: Examples in the text assume the reader is at least an advanced beginner in Python programming.

The lambda construct allows you to create anonymous functions on the fly. Since methods are first-class objects in Python, you can pass anonymous functions around as arguments to a method. Practically speaking, because methods are first-class objects, you can assign a method to a variable just as you would an instance of a class. (If you've got a C background, passing a method in this manner is essentially the same as a pointer to a function.) This ability to use method objects as arguments to a function makes it simple to implement callbacks.

Figure 1 illustrates the syntax and usage of the lambda.

Figure 1: Syntax and Usage of the Lambda Construct

Though the example in Figure 1 is trivial, you can see that using lambda eliminates the need for an additional if conditional block. In more complex code, consolidating conditional logic into small helper lambda functions (that you don't need to define as methods in their own right) can make code more structured and readable.

If the concept of a callback is unfamiliar to you, consider another example: the Python standard library's os.path.walk(root, function, args ) method. The walk() function uses the passed argument (args) to apply the method's function argument as the code recursively iterates through a directory tree starting at the specified root. This repeated calling out (or back) to the same function while executing the loop may be how the technique got the name callback, but don't quote me on that.

You'll find a practical implementation of the walk( ) method in a previously published DevX article I wrote Python Boosts Jar Auditor Functionality. The auditor utility highlighted in that article presents another useful Python feature as well: the ability to create closures. Objects are data bundled with behavior (methods); closures are methods bundled with some data. Python enables you to create closures by allowing methods to set initial argument values in the method definition itself (see Figure 2).

Figure 2: Create Closures in Python

In the Figure 2 snippet, Python presets the y value. This way, if a value isn't passed to the method (under some condition), the method uses the value set in the method definition. This adds great flexibility to the way methods are called. This isn't just syntactic sugar. The added expressive power, depending upon the circumstances, can simplify or reduce the coded logic required to set the argument(s) that need to be passed.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date