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


Groups > comp.lang.python > #16015

Re: decorators and closures

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'desirable.': 0.07; 'decorator': 0.09; 'defined.': 0.09; 'incompatible': 0.09; 'question?': 0.09; 'def': 0.13; 'dec:': 0.16; 'executed,': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; '(which': 0.19; 'cc:no real name:2**0': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'function': 0.27; 'tried': 0.27; 'variable': 0.28; 'generic': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'example': 0.29; 'definition': 0.30; 'usually': 0.31; "didn't": 0.31; 'change.': 0.32; 'changes': 0.32; 'received:209.85.161.46': 0.32; 'received :mail-fx0-f46.google.com': 0.32; "isn't": 0.33; 'message- id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'nested': 0.34; 'sense,': 0.34; 'received:209.85.161': 0.36; 'received:google.com': 0.37; 'another': 0.37; 'think': 0.37; 'received:10.0.0': 0.38; 'received:209.85': 0.38; 'why': 0.39; 'help': 0.39; 'received:209': 0.40; 'more': 0.61; 'your': 0.61; 'proved': 0.67; 'discovered': 0.68; 'time..': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=cUH8fxqWksvqztougFgsZPf9illL9FxPReKUnQAWOMg=; b=I9b2C3wYL5WVHtf61kbOkQSSPUHR4iEFykyZbsLqTOQxoCTkPR1KdAAop2iMZZmrrw Eqczc6DQIsu0LzAwOFClps2v4Sy3B5jvafeSGdBzdoR3hU4UFqk+oidqNnA4uVDmTMn5 LiapdlQfBiJbp9BpRkArYewLe7sy0200s3aCs=
Date Mon, 21 Nov 2011 15:35:13 +0000
From Andrea Crotti <andrea.crotti.0@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111108 Thunderbird/8.0
MIME-Version 1.0
To d@davea.name
Subject Re: decorators and closures
References <4ECA63D2.7080603@gmail.com> <4ECA68D8.2060608@davea.name>
In-Reply-To <4ECA68D8.2060608@davea.name>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2905.1321889718.27778.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321889718 news.xs4all.nl 6939 [2001:888:2000:d::a6]:54092
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16015

Show key headers only | 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