What is "@" used for ?

G

gops

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
 
L

Lie

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.
 

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,599
Members
45,165
Latest member
JavierBrak
Top