scope of function parameters (take two)

Daniel Kluev dan.kluev at gmail.com
Tue May 31 20:04:10 EDT 2011


On Wed, Jun 1, 2011 at 3:16 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>
> There is no "decorator" module in the standard library.  This must be
> some third-party module.  The usual way to do this would be:

Yes, but its very useful for decorators and provides some
not-readily-available functionality.
http://pypi.python.org/pypi/decorator/3.3.1

> 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".

No, it will not. Its the magic of decorator library, it is
signature-preserving, while your variant breaks function signature and
causes problems to any code that relies on signatures (happens with
Pylons, for example).

>>> @copy_args
... def test(a, f=None):
...     print f
...
>>> test([], f=123)
123

Basically decorator.decorator uses exec to create new function, with
signature of function you pass to your decorator, so it does not
matter what names you used for args in decorator itself.

-- 
With best regards,
Daniel Kluev



More information about the Python-list mailing list