what is meaning of "@" in pyhon program.

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Jun 27 11:09:51 EDT 2008


Mike Driscoll a écrit :
> On Jun 27, 9:48 am, Evan <xdi... at gmail.com> wrote:
>> 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




More information about the Python-list mailing list