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


Groups > comp.lang.python > #16015

Re: decorators and closures

Date 2011-11-21 15:35 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
Subject Re: decorators and closures
References <4ECA63D2.7080603@gmail.com> <4ECA68D8.2060608@davea.name>
Newsgroups comp.lang.python
Message-ID <mailman.2905.1321889718.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 11/21/2011 03:06 PM, Dave Angel wrote:
> Your function 'nested' isn't nested, 'fun' is.  What you discovered is 
> that a decorator is always executed, every time a nested decorated 
> function is defined.
>
> You've also ust proved that it would be an incompatible change.  
> Doesn't that answer the question?  An optimizer that changes the 
> behavior isn't usually desirable.
>
Yes sure I think it makes perfectly sense, because you actually redefine 
a local variable every time..

Another thing (which was also the reason of the subject), I tried to 
disassemble the following:
   def dec(fn):
       def _dec():
           fn()

       return _dec

   @dec
   def fun():
       print("here")

   fun()

And I get this:
   In [29]: dis.dis(test_decorate)
   Disassembly of dec:
     2           0 LOAD_CLOSURE             0 (fn)
                 3 BUILD_TUPLE              1
                 6 LOAD_CONST               1 (<code object _dec at 
0x1c4b930, file "test_decorate.py", line 2>)
                 9 MAKE_CLOSURE             0
                12 STORE_FAST               1 (_dec)

     5          15 LOAD_FAST                1 (_dec)
                18 RETURN_VALUE

   Disassembly of fun:
     3           0 LOAD_DEREF               0 (fn)
                 3 CALL_FUNCTION            0
                 6 POP_TOP
                 7 LOAD_CONST               0 (None)
                10 RETURN_VALUE


Looking up the definition of the single calls didn't help much, so why 
do we need
for example MAKE_CLOSURE?
Is MAKE_CLOSURE just more generic maybe?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: decorators and closures Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-11-21 15:35 +0000

csiph-web