Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the []-esque decorator syntax in Python mean?

Here's a snippet of code from within TurboGears 1.0.6:

[dispatch.generic(MultiorderGenericFunction)]
def run_with_transaction(func, *args, **kw):
    pass

I can't figure out how putting a list before a function definition can possibly affect it.

In dispatch.generic's docstring, it mentions:

Note that when using older Python versions, you must use '[dispatch.generic()]' instead of '@dispatch.generic()'.

OK, so it apparently is a way to get decorator-like behavior in pre-decorator versions of Python, but how the heck can it possibly work?

like image 977
Michael Gundlach Avatar asked Dec 18 '08 21:12

Michael Gundlach


People also ask

What is the meaning of decorator in Python?

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

What is the correct syntax for using a decorator?

Decorators use a special syntax in JavaScript, whereby they are prefixed with an @ symbol and placed immediately before the code being decorated.

What are the types of decorators in Python?

In fact, there are two types of decorators in Python — class decorators and function decorators — but I will focus on function decorators here.

What is the Python decorator give an example?

You'll use a decorator when you need to change the behavior of a function without modifying the function itself. A few good examples are when you want to add logging, test performance, perform caching, verify permissions, and so on. You can also use one when you need to run the same code on multiple functions.


1 Answers

The decorator syntax is provided by PyProtocols.

""" Finally, it's important to note that these "magic" decorators use a very sneaky hack: they abuse the sys.settrace() debugger hook to track whether assignments are taking place. Guido takes a very dim view of this, but the hook's existing functionality isn't going to change in 2.2, 2.3, or 2.4, so don't worry about it too much. This is really a trick to get "early access" to decorators, and the 2.4 lifecycle will be plenty long enough to get our code switched over to 2.4 syntax. Somewhere around Python 2.5 or 2.6, add_assignment_advisor() can drop the magic part and just be a backward compatibility wrapper for the decorators that use it. """ http://dirtsimple.org/2004/11/using-24-decorators-with-22-and-23.html

So it sounds like these work by wrapping the actual decorator in some magic that hooks into special code for debuggers to manipulate what actually gets assigned for the function.

The python docs say this about settrace

""" Note The settrace() function is intended only for implementing debuggers, profilers, coverage tools and the like. Its behavior is part of the implementation platform, rather than part of the language definition, and thus may not be available in all Python implementations. """

like image 113
Ed. Avatar answered Oct 30 '22 03:10

Ed.