Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6737

Re: scope of function parameters (take two)

References <F8395F78-615E-4FBD-B6FC-1D6173EAEA45@mcgill.ca> <F4EAD1ED-563D-4D6E-A50C-68308A9F26B7@mcgill.ca> <BANLkTin5exEpDkE3on3BAaWwHOjpg_vC8g@mail.gmail.com> <6699AB10-988A-49AD-B7C1-6BAA2CC3D008@mcgill.ca> <BANLkTikH0S4TNAjZzfAj6mAQoTeXLYFpgQ@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-05-31 10:16 -0600
Subject Re: scope of function parameters (take two)
Newsgroups comp.lang.python
Message-ID <mailman.2319.1306858604.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, May 31, 2011 at 1:38 AM, Daniel Kluev <dan.kluev@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

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: scope of function parameters (take two) Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-31 10:16 -0600

csiph-web