variables preceeded with @

Diez B. Roggisch deets at nospam.web.de
Tue Feb 7 09:24:25 EST 2006


Thomas Girod wrote:

> Hello there.
> 
> I recently started looking at turbogears and I found code such as :
> 
>  class Root(controllers.Root):
>      @turbogears.expose(html="blog.templates.accueil")
>      def index(self,**kw):
>          return dict()
> 
> 
> What is this "@" ? I looked around but couldn't find any references to
> this syntax.

It is called a decorator syntax, and is basically syntactic sugar for the
following:

class Foo(object):
    def bar(self):
        pass

    bar = decorate(bar)

is 

class Foo(object):
    @decorate
    def bar(self):
        pass


You can also have parameterized decorators, as the ones in TG:

class Foo(object):
    def bar(self):
        pass

    bar = decorate_with_args("some argument")(bar)

is 

class Foo(object):
    @decorate_with_args("some argument")
    def bar(self):
        pass


I suggest you read this to get on speed with decorators:

http://www.phyast.pitt.edu/~micheles/python/documentation.html



Regards,

Diez




More information about the Python-list mailing list