Browse DevX
Sign up for e-mail newsletters from DevX


Power Python: Do More with Less Code : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reduce( )
The reduce(afunction, alist) function is similar to map( ) and filter( ) in its signature. However, reduce() serves to aggregate the members of a list, typically to return a single value.

Imagine that you are assembling URLs dynamically for a list of article links. The parts of the URL are in a list that specifies the location, the path where they will reside on the host, and finally, the file name (see Figure 5). (This is only an example. This article is exclusively available on DevX.com).

Figure 5: Assembling URLs Dynamically for a List

If you're unfamiliar with the operator module, it is mostly composed of the basic Python operators converted into a function form. While a function version of an operator isn't going to perform better than the built-in operator, it does have its advantages for certain types of operations. In the Figure 5 example, join(url_parts,'') would have worked just as well.

List Comprehension
The list comprehension construct is really just syntactic sugar to make operations that work on lists simpler to write and understand. It fits with the other functional constructs because it expresses the iteration through the members of a list as a single thought in one line of code (see Figure 6).

Figure 6: List Comprehension Loop

The loop in Figure 6 reads as follows: "Return a list of two element tuples where the multiplier(x,y) function results in a value greater than 25 using the combination of inputs from the tuples (1, 4, 6, 24, 19) and (15, 7, 1, 2)."

Although this syntax may be unfamiliar to you, hopefully you recognize that the example list comprehension just built a concise nested for loop. The advantages here are:

  • Fewer lines of code reduce the opportunity to introduce a bug.
  • The code is arguably simpler to write since no indentations are required and the extent of the loop is delimited by the square brackets ( [ ] ).
  • The return value is stated up front (the tuple (x,y) at the start of the line), which eliminates the need to determine which form the output result of the loop will take.

On the flip side, while this syntax enables a programmer to write the loop in a single line, it perhaps appears more cluttered to somebody unfamiliar with list comprehensions, which could impact maintainability. Also, it is more difficult to insert intermediate debug print statements.

Do More with Less
Python offers some elegant constructs that enable you to write clearer, more concise code to get the job done. As with any coding technique, misuse will undermine the readability and maintainability of your code. However, properly implemented, these techniques offer a way to more directly translate your thoughts into clean, concise, and expressive code. Of course, you should use the right tool for the job, but if the job is a fit for Python, then these techniques will enable you to do more with less.

Doug Tillman is a veteran Java and Python developer with Grainger.com. He is currently working on a Zope and Plone implementation.
Thanks for your registration, follow us on our social networks to keep up-to-date