variables preceeded with @

T

Thomas Girod

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.

Thomas
 
D

Diez B. Roggisch

Thomas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,283
Messages
2,571,409
Members
48,103
Latest member
MadieDeitz

Latest Threads

Top