scope of function parameters (take two)

Chris Kaynor ckaynor at zindagigames.com
Tue May 31 12:34:34 EDT 2011


On Tue, May 31, 2011 at 9:16 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:

> On Tue, May 31, 2011 at 1:38 AM, Daniel Kluev <dan.kluev at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110531/be588759/attachment-0001.html>


More information about the Python-list mailing list