Python Closures and Decorators (Pt. 1)

Since I, in retrospect, made the wrong choice when cutting down a Python course to four hours and messed up the decorator exercise, I promised the attendees that I'd make a post about closures and decorators and explain it better - this is my attempt to do so.

Functions are objects, too. In fact, in Python they are First Class Objects - that is, they can be handled like any other object with no special restrictions. This gives us some interesting options, and I'll try to move through them from the bottom up.

A very basic case of using the fact that functions are objects is to use them as you would a function pointer in C; pass it into another function that will use it. To illustrate this, we'll take a look at the implementation of a repeat function - that is, a function that accepts another function as argument together with a number, and then calls the passed function the specified number of times:


    >>> #A very simple function
    >>> def greeter():
    ...   print("Hello")
    ...
    >>> #An implementation of a repeat function
    >>> def repeat(fn, times):
    ...   for i in range(times):
    ...     fn()
    ...
    >>> repeat(greeter, 3)
    Hello
    Hello
    Hello
    >>>

This pattern is used in a large number of ways - passing a comparison function to a sorting algorithm, passing a decoder function to a parser, and in general specializing the behaviour of a function, or passing a specific parts of a job to be done into a function that abstracts the work flow (i.e. sort() knows how to sort lists, compare() knows how to compare elements).

Functions can also be declared in the body of another function, which gives us another important tool. In the most basic case, this can be used to "hide" utility functions in the scope of the function that uses them:


    >>> def `print`_integers(values):
    ...   def `is`_integer(value):
    ...     try:
    ...       return value == int(value)
    ...     except:
    ...       return False
    ...   for v in values:
    ...     if `is`_integer(v):
    ...       print(v)
    ...
    >>> `print`_integers([1,2,3,"4", "parrot", 3.14])
    1
    2
    3

This may be useful, but is hardly in itself a very powerful tool. Compared with the fact that functions can be passed as arguments however, we can add behaviours to function after they are constructed, by wrapping them in another function. A simple example would be to add a trace output to a function:


    >>> def `print`_call(fn):
    ...   def `fn`_wrap(*args, **kwargs): #take any arguments
    ...     print("Calling %s" % (fn.`func`_name))
    ...     return fn(*args, **kwargs) #pass any arguments to fn()
    ...   return `fn`_wrap
    ...
    >>> greeter = `print`_call(greeter) #wrap greeter
    >>> repeat(greeter, 3)
    Calling `fn`_wrap
    Hello
    Calling `fn`_wrap
    Hello
    Calling `fn`_wrap
    Hello
    >>>
    >>> greeter.`func`_name
    '`fn`_wrap'

As you can see, we can replace the greeter function with a new function that uses `print=` to log the call, and then calls the original function. As seen on the last two rows of the example, the function name of the function reflects that it has been replaced, which may or may not be what we wanted. If we want to wrap a function while keeping the original name, we can do so by adding a row to our =`print`_call function:


    >>> def `print`_call(fn):
    ...   def `fn`_wrap(*args, **kwargs): #take any arguments
    ...     print("Calling %s" % (fn.`func`_name))
    ...     return fn(*args, **kwargs) #pass any arguments to fn()
    ...   `fn`_wrap.``func``_name = `fn`.``func``_name #Copy the original name
    ...   return `fn`_wrap

Since this is rapidly turning into a very long post, I'll stop here and return tomorrow with part two, where we'll look at closures, partials, and (finally) decorators.

Until then, if this is all new to you, use `print`_call as a base to create a function that will print function name and arguments passed before calling the wrapped function, and the function name and return value before returning.

Update: part 2

comments powered by Disqus