6 Techniques I Learned To Use While Working More With Python

These last months, I learned more about many Python features and how to write Pythonic code that really uses those features. That happened naturally. I wanted to get better at programming with Python for my projects, and I started watching Pycon talks on a daily basis or reading the documentation and online resources. Also, in current projects, I am mostly using Python 3.

Here are examples showing 6 of the features and techniques I have been using more and more, and which improve the quality of my code, as well as maintainability.

String formating

There are several ways of doing, but this one is quite nice, once you get used to it:

firstname = 'John'
lastname = 'Doe'
print('{0} {1}'.format(firstname, lastname))

For more details, check the string formats documentation.

The enumerate() function

Sometimes you need to iterate through a list and get the index or position of each member and the members at the same time. The trick is to use the enumerate() builtin function. You get the pairs index and member element.

mylist = ['a','b','c','d','e','f']
for idx, item in enumerate(mylist):
    print('{0}: {1}'.format(idx, item))

For more details, check the builtin functions documentation.

Context managers

The Context managers feature allow accessing things such as files or network resources, and releasing them when you are done, using the with statement.

As a common example, when opening a file, instead of the old way...

myfile = open("file.txt")

# Do something with the file
lines = myfile.readlines()
print(lines)

# Now, close the handler on the file
myfile.close()

... you would do:

with open("file.txt") as myfile:
    # Do something with the file
    # ...

This is the Python way!

There is more to Python's context managers. For details, you can start here and here.

Iterators

The iterator feature offers a kind of generalization of the behavior of a list, when looping over an object. You can iterate, using the for loop, through many types of objects, not only lists. For example, strings and dictionaries.

Objects having that property are called iterable objects.

Here is an example, where opening a text file produces an iterator with the lines of the file:

with open("file.txt") as myfile:
    for line in myfile:
        print(line)

An important thing with iterators is that the elements are not loaded into memory at once, which would be suboptimal for large lists. They are loaded one by one, when called.

There are helper functions such as list() for cases where you do want to get the entire list at once, and also iter() for when you want to transform an object into an iterator.

You can also use functions such as min(), max(), sum() to quickly produce results when handling an iterator.

Iterators are commonly used in Python, and the itertools builtin module offers even more functions for creating and working with iterators.

The named tuple container type

A named tuple is a lightweight object type you can use, instead of the standard tuple or the dictionnary type, to store and manipulate data, when you don't want to define a new object class.

Here is the basis of how we define and use a named tuple for product information we would get from a hypothetical data source:

from collections import namedtuple

Product = namedtuple('Product', 'name price url')

p = Product(name='An example product', price='19.99', url='http://example.com/products/example-product')

print(p.name)
print(p.price)
print(p.url)

I have been using this feature when writing web scraping code where I need to do stuff with the data collected until a point where I eventually save the data on disk to a CSV file.

For more details, check the container datatypes documentation.

The deque container type

Another useful data structure is deque. From the documentation, it is a "list-like container with fast appends and pops on either end".

Here is an example with a list that you get and need to maintain, by transforming it into a deque in order to extend it at the left end.

from collections import deque

team = ['john', '32', 'jim', '28', 'gary', '35']
data = deque(team)
data.extendleft(['26', 'alfred'])
data.appendleft('team members')

For more details, check the container datatypes documentation.

And more...

This is just a selection. There are tons of nice features and Pythonic ways of writing code, you get the opportunity to learn while practicing.

comments powered by Disqus

Need help for your project?

Our team can contribute to your project, working on a specific task, or doing all the coding based on your specifications, using Python, a web framework such as Django or a CMS.