Using decorators with argument in Python

John Posner jjposner at codicesoftware.com
Wed Jun 29 13:23:08 EDT 2011


On 2:59 PM, Lie Ryan wrote:
>> Can any of you guys explain me advantages and disadvantages of
>> using each of them
> Simplicity is one, using @decor() means you have at least three-level
> nested functions, which means the code is likely to be very huge and
> perhaps unnecessarily.

Bruce Eckel pointed out (
http://www.artima.com/weblogs/viewpost.jsp?thread=240808) that the
result of "decoration" need not be a function. Instead, it can be an
object (instance of a user-defined class) that's callable (because the
class implements a __call__ method).

Investigating how this fact fit in with the current thread, I came up
with an alternative to the three levels of "def" (pronounced "three
levels of death"). Following is code for two decorators:

* the first one encloses the output of a function with lines of "#"
characters, and is used like this:

     @enclose
     myfun(...

* the second one encloses the output of a function with lines of a
user-specified character, and is used like this:

     @enclose("&")
     myfun(...

Here's the Python2 code for each one:

################## decorator to be called with no argument

class enclose:
    """
    class that can be used as a function decorator:
    prints a line of "#" before/after the function's output
    """
    def __init__(self, funarg):
        self.func = funarg
    def __call__(self, *args, **kwargs):
        print "\n" + "#" * 50
        self.func(*args, **kwargs)
        print "#" * 50 + "\n"

################## decorator to be called with argument

def enclose(repeat_char):
    """
    function that returns a class that can be used as a decorator:
    prints a line of <repeat_char> before/after the function's output
    """
    class _class_to_use_as_decorator:
        def __init__(self, funarg):
            self.func = funarg
        def __call__(self, *args, **kwargs):
            print "\n" + repeat_char * 50
            self.func(*args, **kwargs)
            print repeat_char * 50 + "\n"

    return _class_to_use_as_decorator


Best,
John




More information about the Python-list mailing list