Using decorators with argument in Python

2011-06-28 Thread Jigar Tanna
I am new to Python and Django, was going through the concept of
decorators
where I came across a special case of using arguments with decorators
Below is the code for memoization where I was looking at the
concept...

cache = {}
def get_key(function, *args, **kw) :
key = '%s. %s: ' % (function. __module__,function. __name__)
hash_args = [ str(arg) for arg in args]
hash_kw = [' %s: %s' % (k, hash(v) )
   for k, v in kw.items() ]
return ' %s:: %s: : %s' % (key, hash_args, hash_kw)

def memoize(get_key=get_key, cache=cache) :
def _memoize( function) :
print function
def __memoize(*args, **kw) :
key = get_key(function, *args, **kw)
try:
return cache[key]
except KeyError:
cache[key] = function( *args, **kw)
return cache[key]
return __memoize
return _memoize

@memoize()
def factory(n) :
return n * n

# testcase
#print factory(3)
#
#

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using decorators with argument in Python

2011-06-29 Thread jigar tanna
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  wrote:

From: Ian Kelly 
Subject: Re: Using decorators with argument in Python
To: "Jigar Tanna" 
Cc: python-list@python.org
Date: Tuesday, 28 June, 2011, 10:50 PM

On Tue, Jun 28, 2011 at 10:52 AM, Jigar Tanna
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using decorators with argument in Python

2011-06-29 Thread jigar tanna


okie i agree with  your comment, if the case is simple we would 
prefer not to make it complex but if required there would be nor harm in
 using decorators with Arguments



Thanks,

J



--- On Tue, 28/6/11, Lie Ryan  wrote:

From: Lie Ryan 
Subject: Re: Using decorators with argument in Python
To: python-list@python.org
Date: Tuesday, 28 June, 2011, 10:36 PM

On 06/29/2011 02:52 AM, Jigar Tanna 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

Simplicity is one, using @decor() means you have at least three-level
nested functions, which means the code is likely to be very huge and
perhaps unnecessarily.

However, there is nothing wrong with using decorators with arguments;
except that if you have a simpler alternative, then why use the more
complex ones?
-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list