« Things I like using python (part II)
24 Jul 2012
Introduction
Here it goes my second and last post on the “things that I like about coding in python” series in which was missing a small section on how cool these features (below) are:
Decorators
Context managers
Use blank spaces to define code blocks
Decorators
A decorator is a function that takes at least an argument, a function object, and returns a single value, a function object. It’s commonly used to, taking advantage of python closures support, add new features to the original function object (the one received as argument).
Before decorators reached python, the following excerpt was needed to create a class/static method:
Reminder: the main difference between static and class methods is that a class method can be overriden by a child, which is not true for a static method. Also a class method needs the class object as first parameter in the method definition. At this point, I don’t find a good reason to define a static method though.
Using decorators and its syntax sugar (the @ symbol), the previous excerpt can be re-written to:
The best blog post explaining decorators I’ve found so far is this one from Steve Ferg. I find decorators quite useful when you need transversal functionalities in your code. A set of examples can be found in wiki.python.org.
Example
The following code creates a decorator that dumps the arguments that a function/method receives when executed. The base of that code is taken from wiki.python.org#Easy_Dump_Of_Function_Arguments.
The execution of the previous code generates the following output:
As it’s shown above (method find_by_id), you can use more than one decorator in a function/method (and are executed bottom-up).
Context managers (with statement)
A context manager allows you to create and manage a run time context. It is created when starting a with statement, it’s available during the code execution inside the with block, and is exited at the end of the with code. The most commonly used scenario is while allocating resources: a context manager ensures you use the resource only while it’s actually required and deallocates it when it should not be used anymore (of course python needs you to write the code properly for that if you are defining your own context manager).
The basic example using python native library to handle a file object:
The file /var/log/events.log is opened when entering into the context manager, and closed when the code block is finished. You don’t need to catch exceptions, close the file, etc.
To create a context manager you need to define a class that implements two methods, __enter__ and __exit__. In the following code I’m creating a context manager, user, that retrieves an object from an external source and stores it back if updated:
Switching to ruby, something similar can be achieved with the following snippet:
Use blank spaces to define code blocks
Not too much to say about this. I thinks it increases readability.
Conclusion
I hope you’ve found these articles interesting. I’m sure some good points, like functions being first-class citizens or the collections and functools modules, are missing but at this point I just wanted to highlight my five coolest features. Let me know which are yours so my top five list could easily be “re-prioritized” :-)