On Tue, May 31, 2011 at 9:16 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Tue, May 31, 2011 at 1:38 AM, Daniel Kluev <dan.kl...@gmail.com> wrote: > > @decorator.decorator > > def copy_args(f, *args, **kw): > > nargs = [] > > for arg in args: > > nargs.append(copy.deepcopy(arg)) > > nkw = {} > > for k,v in kw.iteritems(): > > nkw[k] = copy.deepcopy(v) > > return f(*nargs, **nkw) > > There is no "decorator" module in the standard library. This must be > some third-party module. The usual way to do this would be: > > def copy_args(f): > @functools.wraps(f) > def wrapper(*args, **kw): > nargs = map(copy.deepcopy, args) > nkw = dict(zip(kw.keys(), map(copy.deepcopy, kw.values()))) > return f(*nargs, **nkw) > return wrapper > > Is there any reason not to simplify this to: def copy_args(f): @functools.wraps(f) def wrapper(*args, **kw): nargs = copy.deepcopy(args) nkw = copy.deepcopy(kw) return f(*nargs, **nkw) return wrapper It means you will copy the keys as well, however they will (almost) certainly be strings which is effectively a no-op. > Note that this will always work, whereas the "decorator.decorator" > version will break if the decorated function happens to take a keyword > argument named "f". > > Cheers, > Ian > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list