What is "@" used for ?

Lie Lie.1296 at gmail.com
Sun Jun 29 12:20:19 CEST 2008


On Jun 29, 3:39 pm, gops <patelgo... at gmail.com> wrote:
> Hi.
>
> I am noob in python. while reading some source code I came across ,
> this funny thing called @ in some function ,
>
> def administrator(method):
>     @functools.wraps(method)
>     def wrapper(self, *args, **kwargs):
>         user = users.get_current_user()
>         if not user:
>             if self.request.method == "GET":
>
> self.redirect(users.create_login_url(self.request.uri))
>                 return
>             raise web.HTTPError(403)
>         elif not users.is_current_user_admin():
>             raise web.HTTPError(403)
>         else:
>             return method(self, *args, **kwargs)
>     return wrapper
>
> now what is that "@" used for ? I tried to google , but it just omits
> the "@" and not at all useful for me(funny!! :D)
>
> It will be enough if you can just tell me some link where i can look
> for it..
>
> Thank you in advance. :D

@ is decorator. It's a syntax sugar, see this example:
class A(object):
    @decorate
    def blah(self):
        print 'blah'

is the same as:
class A(object):
    def blah(self):
        print 'blah'
    blah = decorate(blah)

Now you know the name, I guess google will help you find the rest of
the explanation.



More information about the Python-list mailing list