advice about `correct' use of decorator

Gerardo Herzig gherzig at fmed.uba.ar
Fri Aug 17 18:15:42 CEST 2007


BJörn Lindqvist wrote:

>On 8/16/07, Gerardo Herzig <gherzig at fmed.uba.ar> wrote:
>  
>
>>@is_logued_in
>>def change_pass():
>>    bla
>>    bla
>>
>>And so on for all the other functions who needs that the user is still
>>loged in.
>>
>>where obviosly the is_logued_in() function will determine if the dude is
>>still loged in, and THEN execute change_pass(). If the dude is not loged
>>in, change_pass() is NOT executed at all. Instead, it will be redirected
>>to the `login' screen.
>>    
>>
>
>I think this is redundant use of a decorator. You can achieve the
>exact same effect by writing:
>
>def is_logued_in():
>    if not user.is_logged_in():
>        raise NotLoggedInError
>
>It costs you one more line, but reduces complexity. And if you are
>worried about that extra line you can put it in a function.
>
>  
>
As far as i know (by the way, AFAK is the shortcut?, and BTW means `by 
the way'? ), decorators are not indispensable. I mean, all that you can 
do with python, you can doit without decorators. And from my point of 
view, this hides the complexity for the other developers of my group, 
since all they have to do is add the @is_logged_in line at the top of 
the cgi script, and not to worrie about exceptions, not even how the 
decorator is implemented (i may log the error in some file). All they 
have to know is that any abnormal situation will redirect to the `login' 
screen.

Thank you for your comments!
Gerardo



More information about the Python-list mailing list