A decorator is just a special type of function that takes a callable and
returns another callable. You typically use it via the "@" syntax, but you
don't have to use it that way.
@auth.requires_login()
def myfunc():
return dict()
is equivalent to:
def myfunc():
return dict()
myfunc = auth.requires_login()(myfunc)
You are simply passing myfunc to a function and getting back a new function.
In the case of auth.requires_login(), we don't really have a function to
decorate, so we just pass in a dummy function (i.e., lambda: None). What we
really want is for auth.requires_login() to execute its redirect logic in
case the user isn't logged in. Otherwise, it should just do nothing and
move on.
Anthony
On Thursday, February 12, 2015 at 4:29:42 PM UTC-5, Tom Campbell wrote:
>
> To take advantage of the decorators at a file level, you can also do this
>> at the top level of a file:
>>
>> auth.requires_login()(lambda: None)()
>>
>> The decorators ultimately call auth.requires, which itself returns a
>> decorator. The above passes a dummy function to that decorator and then
>> simply calls it. Note, you don't prepend with "@" in this case, as you are
>> not decorating a Python function.
>>
>> I think that's brilliant... but I don't quite understand. Without the
> decorators how to the functions in the file get forced to go through
> auth.requires_login()?
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.