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


Groups > comp.lang.python > #6737 > unrolled thread

Re: scope of function parameters (take two)

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2011-05-31 10:16 -0600
Last post2011-05-31 10:16 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#6737 — Re: scope of function parameters (take two)

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-31 10:16 -0600
SubjectRe: scope of function parameters (take two)
Message-ID<mailman.2319.1306858604.9059.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web