Re: Using arguments in a decorator

2012-04-21 Thread Rotwang
On 21/04/2012 09:36, Steven D'Aprano wrote: [...] Here is how I would write the above. import functools def memoise(func): """Decorator to memoise a function.""" cache = {} @functools.wraps(func) def inner(*args, **kwargs): # Make sure keyword args are always look

Re: Using arguments in a decorator

2012-04-21 Thread Jon Clements
On Saturday, 21 April 2012 09:25:40 UTC+1, Steven D'Aprano wrote: > On Fri, 20 Apr 2012 09:10:15 -0700, Jon Clements wrote: > > >> But I don't know how. I know that I can see the default arguments of > >> the original function using func.__defaults__, but without knowing the > >> number and names

Re: Using arguments in a decorator

2012-04-21 Thread Steven D'Aprano
On Fri, 20 Apr 2012 16:57:06 +0100, Rotwang wrote: > def memo(func): > def memofunc(*args, **kwargs): > twargs = tuple(kwargs.items()) > if (args, twargs) in memofunc.d: > return copy(memofunc.d[(args, twargs)]) > memofunc.d[(args, twargs)] = func(*args

Re: Using arguments in a decorator

2012-04-21 Thread Steven D'Aprano
On Fri, 20 Apr 2012 09:10:15 -0700, Jon Clements wrote: >> But I don't know how. I know that I can see the default arguments of >> the original function using func.__defaults__, but without knowing the >> number and names of func's positional arguments (which I don't know how >> to find out) this

Re: Using arguments in a decorator

2012-04-20 Thread Ian Kelly
On Fri, Apr 20, 2012 at 6:07 PM, Ian Kelly wrote: >    (args, varargs, varkw, defaults) = inspect.getargspec(func) >    if varargs: >        args.append(varargs) >    if varkw: >        args.append("tuple(sorted(%s.items()))" % varkw) Note that in Python 3, this would need to become something lik

Re: Using arguments in a decorator

2012-04-20 Thread Ian Kelly
On Fri, Apr 20, 2012 at 9:57 AM, Rotwang wrote: > As far as I know, the decorated function will always return the same value > as the original function. The problem is that the dictionary key stored > depends on how the function was called, even if two calls should be > equivalent; hence the origi

Re: Using arguments in a decorator

2012-04-20 Thread Rotwang
On 20/04/2012 17:10, Jon Clements wrote: On Friday, 20 April 2012 16:57:06 UTC+1, Rotwang wrote: Hi all, here's a problem I don't know how to solve. I'm using Python 2.7.2. I'm doing some stuff in Python which means I have cause to call functions that take a while to return. Since I often want

Re: Using arguments in a decorator

2012-04-20 Thread Peter Otten
Rotwang wrote: > I've written a > decorator to eliminate repeated calls by storing a dictionary whose > items are arguments and their results: > The problem is that the dictionary key > stored depends on how the function was called, even if two calls should > be equivalent; hence the original fun

Re: Using arguments in a decorator

2012-04-20 Thread Jon Clements
On Friday, 20 April 2012 16:57:06 UTC+1, Rotwang wrote: > Hi all, here's a problem I don't know how to solve. I'm using Python 2.7.2. > > I'm doing some stuff in Python which means I have cause to call > functions that take a while to return. Since I often want to call such a > function more th