what is meaning of "@" in pyhon program.

E

Evan

HI,

When I check example of "cmd2" module (a enhancement of cmd module), I
can not understand all, for example: the character "@",

+++++++++++++++++++++++++++++++++++++++++++++++++++++
def options(option_list):
..........<function content>........

class cmd(...):
...............................
@options([make_option('-p', '--piglatin', action="store_true",
help="atinLay")])
+++++++++++++++++++++++++++++++++++++++++++++++++++++

I do not understand what "@options" does, most time, I know what the
meaning of character "*" and character "**", but I was not use "@"
before.

Thanks for your help.
 
M

Mike Driscoll

HI,

When I check example of "cmd2" module (a enhancement of cmd module), I
can not understand all, for example: the  character "@",

+++++++++++++++++++++++++++++++++++++++++++++++++++++
def options(option_list):
     ..........<function content>........

class cmd(...):
    ...............................
    @options([make_option('-p', '--piglatin', action="store_true",
help="atinLay")])
+++++++++++++++++++++++++++++++++++++++++++++++++++++

I do not understand what "@options" does,   most time, I know what the
meaning of character "*" and character "**", but I was not use "@"
before.

Thanks for your help.

The "@" sign is there to signify that what follows is a decorator.
They're kind of cool and kind of confusing. Basically they dynamically
alter a function, method or class and gives them additional
functionality. It's kind of like calling a function recursively. Check
out the following links:

http://www.python.org/dev/peps/pep-0318/
http://wiki.python.org/moin/PythonDecorators
http://www.ibm.com/developerworks/linux/library/l-cpdecor.html

Mike
 
B

Bruno Desthuilliers

Mike Driscoll a écrit :
HI,

When I check example of "cmd2" module (a enhancement of cmd module), I
can not understand all, for example: the character "@",

+++++++++++++++++++++++++++++++++++++++++++++++++++++
def options(option_list):
..........<function content>........

class cmd(...):
...............................
@options([make_option('-p', '--piglatin', action="store_true",
help="atinLay")])
+++++++++++++++++++++++++++++++++++++++++++++++++++++

I do not understand what "@options" does, most time, I know what the
meaning of character "*" and character "**", but I was not use "@"
before.

Thanks for your help.

The "@" sign is there to signify that what follows is a decorator.
They're kind of cool and kind of confusing. Basically they dynamically
alter a function, method or class and gives them additional
functionality. It's kind of like calling a function recursively.

Mostly, it's just syntactic sugar for an higher order function. IOW, given:

def deco(func):
# do anythin you wish here
# and return any callable object
return any_callable_object

the two following syntaxes are equivalent:

def toto():
pass
toto = deco(toto)

@deco
def toto():
pass
 
D

Damon Getsman

Okay, maybe I just didn't understand the websites that were given as
examples as to 'decoration'. I first came across the unusual '@' when
I was browsing through some extreme beginner's information on os.x
method descriptions. I asked some other people about it and they had
no idea what it meant. I don't _THINK_ that the decoration definition
fits, though, because the examples that I saw it in had it prefixing
an if conditional & a for loop.

ie:
@if os.exists(foo):
etc
etc

and

@for blah:
etc
etc

does this mean that the person writing the script defined a function
instead of the standard conditional and loop? I can't imagine that
would be the case because, as I said, this was very beginning level
documentation.

-Damon Getsman
 
B

Bruno Desthuilliers

Damon Getsman a écrit :
Okay, maybe I just didn't understand the websites that were given as
examples as to 'decoration'. I first came across the unusual '@' when
I was browsing through some extreme beginner's information on os.x
method descriptions. I asked some other people about it and they had
no idea what it meant. I don't _THINK_ that the decoration definition
fits, though, because the examples that I saw it in had it prefixing
an if conditional & a for loop.

ie:
@if os.exists(foo):
etc
etc

and

@for blah:
etc
etc

This is not valid Python. period.
 
J

John Salerno

Damon Getsman said:
Okay, maybe I just didn't understand the websites that were given as
examples as to 'decoration'. I first came across the unusual '@' when
I was browsing through some extreme beginner's information on os.x
method descriptions. I asked some other people about it and they had
no idea what it meant. I don't _THINK_ that the decoration definition
fits, though, because the examples that I saw it in had it prefixing
an if conditional & a for loop.

The OP's code sample makes sense for decorators, I think. Can you post an
actual sample of what it is you saw? Sounds weird.
 
D

Damon Getsman

I didn't think that it was. I just spent about 10 minutes trying to
google for the page that I found that on, but I wasn't able to turn it
up. My google-fu sucks. I know that I was researching a particular
part of the os module and that the snippets of script on the page had
an example containing '@if' and '@for'. I don't remember what exactly
I used to turn it up before, though, and the link isn't in my history,
it was probably a month ago that I saw it.

Sorry, I tried.
 
A

Aahz

Okay, maybe I just didn't understand the websites that were given as
examples as to 'decoration'. I first came across the unusual '@' when
I was browsing through some extreme beginner's information on os.x
method descriptions. I asked some other people about it and they had
no idea what it meant. I don't _THINK_ that the decoration definition
fits, though, because the examples that I saw it in had it prefixing
an if conditional & a for loop.

ie:
@if os.exists(foo):
etc
etc

and

@for blah:
etc
etc

Guessing:

This is either a source document of text where "@" is some kind of markup
indicating a keyword (so that keywords can be e.g. bolded in the output)
or this is a snippet of code from some kind of Python-based HTML
templating language where "@" is used to indicate Python keywords that
need to be interpreted instead of static text.
 
T

Thierry

ie:
@if os.exists(foo):
   etc
   etc

and

@for blah:
   etc
   etc

This sounds more like PHP code, where a @ prefixing a function means
that even if there are errors or warnings, you don't want to see them.
 
C

cokofreedom

This sounds more like PHP code, where a @ prefixing a function means
that even if there are errors or warnings, you don't want to see them.

Could also by Doxygen, doesn't it use something similar to show
keywords in documents?
 

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
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top