background preloader

Python decorators

Facebook Twitter

Charming Python: Decorators make magic easy. Doing a lot by doing very little Decorators have something in common with previous metaprogramming abstractions introduced to Python: they do not actually do anything you could not do without them.

Charming Python: Decorators make magic easy

As Michele Simionato and I pointed out in earlier Charming Python installments, it was possible even in Python 1.5 to manipulate Python class creation without the "metaclass" hook. Decorators are similar in their ultimate banality. All a decorator does is modify the function or method that is defined immediately after the decorator. This was always possible, but the capability was particularly motivated by the introduction of the classmethod() and staticmethod() built-in functions in Python 2.2. Listing 1. Class C: def foo(cls, y): print "classmethod", cls, y foo = classmethod(foo) Though classmethod() is a built-in, there is nothing unique about it; you could also have "rolled your own" method transforming function. Listing 2. Listing 3.

Listing 4. Listing 5. Listing 6. Listing 7. Back to top. Python Decorators Don't Have to be (that) Scary. Decorators modify functions.

Python Decorators Don't Have to be (that) Scary

Beginning with the basics, learn how to use decorators in a variety of ways. Execute code when a function is parsed or called. Conditionally call functions and transform inputs and outputs. Write customizable decorators that accept arbitrary arguments. And, if necessary, easily make sure your decorated function has the same signature as the original. Decorators. Okay, maybe not. 1 The Basics Decorators modify functions. When you use a decorator, Python passes the decorated function -- we'll call this the target function -- to the decorator function, and replaces it with the result. . # 's 1def decorator_function(target): 2 # Do something with the target function 3 target.attribute = 1 4 return target 5 6def target(a,b): 7 return a + b 8 9# This is what the decorator actually does10target = decorator_function(target) This code has the exact same functionality, but uses decorators.

Both of the above examples will have the same results: No. 2 Run-Time Transformations. Decorators I: Introduction to Python Decorators. Computing ThoughtsDecorators I: Introduction to Python Decoratorsby Bruce EckelOctober 18, 2008 Summary This amazing feature appeared in the language almost apologetically and with concern that it might not be that useful. I predict that in time it will be seen as one of the more powerful features in the language. The problem is that all the introductions to decorators that I have seen have been rather confusing, so I will try to rectify that here. (This series of articles will be incorporated into the open-source book Python 3 Patterns & Idioms). First, you need to understand that the word "decorator" was used with some trepidation, because there was concern that it would be completely confused with the Decorator pattern from the Design Patterns book.

Indeed, you can use Python decorators to implement the Decorator pattern, but that's an extremely limited use of it. The macro has a long history, but most people will probably have had experience with C preprocessor macros. The output is: