Using decorators with argument in Python
Ian Kelly
ian.g.kelly at gmail.com
Tue Jun 28 13:20:29 EDT 2011
On Tue, Jun 28, 2011 at 10:52 AM, Jigar Tanna
<poisonousrattle5 at gmail.com> wrote:
> coming across to certain views from people, it is not a good practice
> to use
> decorators with arguments (i.e. @memoize() ) and instead it is good to
> just
> use @memoize. Can any of you guys explain me advantages and
> disadvantages of
> using each of them
The main concern I think is not with how the decorators are used but
how they are designed. An argument-less decorator will normally be
used as @memoize, and @memoize() will likely not work. A decorator
with arguments that are all optional will normally be used as
@memoize(), and @memoize will likely not work. This naturally leads
to some confusion: do I need parentheses to use this particular
decorator or not?
As a personal design goal I try to make my decorators either take at
least one required argument or take no arguments at all. This way
it's either @memoize or @memoize(foo), but never just the confusing
@memoize().
Cheers,
Ian
More information about the Python-list
mailing list