Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'interpreter': 0.05; 'desirable.': 0.07; 'decorator': 0.09; 'defined.': 0.09; 'function:': 0.09; 'incompatible': 0.09; 'question?': 0.09; 'am,': 0.12; 'def': 0.13; 'betting': 0.16; 'executed,': 0.16; 'received:192.168.1.104': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'code': 0.25; "i'm": 0.26; 'function': 0.27; 'cc:addr:python.org': 0.29; 'usually': 0.31; 'change.': 0.32; 'changes': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.33; 'possible,': 0.34; 'nested': 0.34; 'but': 0.37; 'received:192': 0.37; "it's": 0.40; 'received:192.168': 0.40; 'your': 0.61; 'proved': 0.67; 'discovered': 0.68; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; '09:44': 0.84; 'andrea': 0.84 Date: Mon, 21 Nov 2011 10:06:00 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 MIME-Version: 1.0 To: Andrea Crotti Subject: Re: decorators and closures References: <4ECA63D2.7080603@gmail.com> In-Reply-To: <4ECA63D2.7080603@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:ZqNWBjmq+geh9SRCcnmd1S9vMCqCk0o7FZnp+kFelLl wQKPCGIwgCjqYpAcbGzDGxRNMH6hj+yUuFS9OJaEjc4/WjxvQz f8QeC0/f7wGGwU1NSxyJPgp1qHq8P9/0CJ0cRUx2EbwPYhbXW1 QLnz8vEsH02QBArFrm5F+eoLRy87WVjeT7QlO9m8ustz/aQsq5 aH8Pj/zIh3D2VO5c7WkevqzQGkdY+nAPL0B1dyRiWK94PYgAYF ps8ymUtzrJ9yGgKZHzp3NqhVoHCeAQZz2crkJI6QhCtKWZtHUI iKHq89p2nzBnOU+HiovGyHJqntWAAcyoaCSJKgC/s5xmYFE/g= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321887989 news.xs4all.nl 6948 [2001:888:2000:d::a6]:43930 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16013 On 11/21/2011 09:44 AM, Andrea Crotti wrote: > With one colleague I discovered that the decorator code is always > executed, every time I call > a nested function: > > def dec(fn): > print("In decorator") > def _dec(): > fn() > > return _dec > > def nested(): > @dec > def fun(): > print("here") > > nested() > nested() > > Will give: > In decorator > In decorator > > So we were wondering, would the interpreter be able to optimize this > somehow? > I was betting it's not possible, but I'm I would like to be wrong :) 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. -- DaveA