[Python-Dev] Decorator order implemented backwards?
James Y Knight
foom at fuhm.net
Tue Aug 10 05:16:39 CEST 2004
I haven't seen anyone remark upon this yet, but the order of decorator
application appears to be implemented backwards.
According to the PEP:
> Proposed Syntax
>
> The current syntax for function decorators as implemented in Python
> 2.4a2 is:
> @dec2
> @dec1
> def func(arg1, arg2, ...):
> pass
>
>
> This is equivalent to:
> def func(arg1, arg2, ...):
> pass
> func = dec2(dec1(func))
However, it seems the '@' form is really doing dec1(dec2(func)), not
dec2(dec1(func)).
The test_decorators.py reveals that it is, indeed, testing *for* the
wrong behavior.
test_decorators.py:
> def test_order(self):
> class C(object):
> @funcattrs(abc=1) @staticmethod
> def foo(): return 42
> # This wouldn't work if staticmethod was called first
> self.assertEqual(C.foo(), 42)
> self.assertEqual(C().foo(), 42)
So, um...I assume this is a bug in the implementation, not the PEP,
because the PEP's way makes sense and the way it's implemented doesn't.
James
More information about the Python-Dev
mailing list