advice about `correct' use of decorator

BJörn Lindqvist bjourne at gmail.com
Fri Aug 17 08:29:46 EDT 2007


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.

-- 
mvh Björn



More information about the Python-list mailing list