@decorator syntax is sugar, but for what exactly?

Bengt Richter bokr at oz.net
Mon Aug 9 01:12:33 CEST 2004


On Sun, 8 Aug 2004 02:00:41 +0200, Tristan Seligmann <mithrandi at mithrandi.za.net> wrote:
[...]
>On Sat, Aug 07, 2004 at 23:50:47 +0000, Bengt Richter wrote:
>
>The code:
>
>>     @limited_expression_producing_function
>>     @another
>>     def func(): pass
>>=20
>
>is equivalent to:
>
>def func(): pass
>func =3D limited_expression_producing_function(another(func))
[...]
I've seen that transformation before ;-)

I'd be interested if someone with 2.4 would do the following (untested):

def A(f): return f
def B(f): return f

def foo():
    @A
    @B
    def bar(): pass
    return bar

def baz():
    def bar(): pass
    return A(B(bar)

import dis
dis.dis(foo)
dis.dis(baz)

Regards,
Bengt Richter



More information about the Python-list mailing list