Why won't this decorator work?

Ian Kelly ian.g.kelly at gmail.com
Sat Jul 2 14:49:15 EDT 2011


On Sat, Jul 2, 2011 at 12:08 PM, John Salerno <johnjsal at gmail.com> wrote:
> But why does the documentation say "The return value of the decorator
> need not be callable"?

Because the language does not enforce the restriction that the return
value should be a callable.  If it's not a callable, then the result
will just be that something non-callable is bound to the "roll_die"
name -- which could be useful, but is probably a bad idea in general.

> And why, if I remove the decorator and just
> leave the two functions as if, does the call to move(roll_die()) work?
> Isn't that what the decorator syntax is essentially doing?

No.  The decorator syntax is doing:

roll_die = move(roll_die)

If you then call roll_die, that is equivalent to "move(roll_die)()",
which is not the same thing as "move(roll_die())".

Cheers,
Ian



More information about the Python-list mailing list