scope of function parameters (take two)

Ian Kelly ian.g.kelly at gmail.com
Tue May 31 21:14:26 EDT 2011


On Tue, May 31, 2011 at 6:04 PM, Daniel Kluev <dan.kluev at gmail.com> wrote:
> 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).

Ah, I see.  I assumed it was much simpler than it is.  I found a way
to break it with Python 3, though:

>>> @copy_args
... def test(*, f):
...     return f
...
>>> test(f=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2, in test
  File "<stdin>", line 6, in copy_args
TypeError: test() needs keyword-only argument f

The interesting thing here is that the decorated function has exactly
the correct function signature; it just doesn't work.

Cheers,
Ian



More information about the Python-list mailing list