Using decorators with argument in Python

jigar tanna jigar_15in at yahoo.co.in
Wed Jun 29 06:50:57 EDT 2011


yes for this case you will have to use @memoize() as all the arguments are optional ...

Thanks,
J


--- On Tue, 28/6/11, Ian Kelly <ian.g.kelly at gmail.com> wrote:

From: Ian Kelly <ian.g.kelly at gmail.com>
Subject: Re: Using decorators with argument in Python
To: "Jigar Tanna" <poisonousrattle5 at gmail.com>
Cc: python-list at python.org
Date: Tuesday, 28 June, 2011, 10:50 PM

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
-- 
http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110629/7e1fb10a/attachment-0001.html>


More information about the Python-list mailing list