Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'interpreter': 0.05; '21,': 0.09; 'decorator': 0.09; 'function:': 0.09; 'reply- to:addr:comp.lang.python': 0.09; 'to:addr:comp.lang.python': 0.09; 'def': 0.13; 'structures': 0.15; 'betting': 0.16; 'decorators.': 0.16; 'executed,': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'programming': 0.21; 'header :In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'code': 0.25; "i'm": 0.26; 'cc:addr:python.org': 0.29; 'header:User-Agent:1': 0.33; 'from:addr:googlemail.com': 0.34; 'possible,': 0.34; 'things': 0.34; 'nested': 0.34; 'but': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'monday,': 0.38; 'received:209.85': 0.38; "it's": 0.40; 'received:209': 0.40; 'might': 0.40; '2011': 0.61; 'love': 0.62; 'discovered': 0.68; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:googlegroups.com': 0.74; 'night': 0.82; 'andrea': 0.84; 'dark': 0.91 Newsgroups: comp.lang.python Date: Tue, 22 Nov 2011 09:03:42 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=1.168.132.125; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: User-Agent: G2/1.0 X-Google-Web-Client: true MIME-Version: 1.0 Subject: Re: decorators and closures From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: comp.lang.python@googlegroups.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321981431 news.xs4all.nl 6969 [2001:888:2000:d::a6]:35251 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16081 On Monday, November 21, 2011 10:44:34 PM UTC+8, 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 :) I love to use decorators. I did the same things to functions and structures in c long time ago. I think that might be the dark night era in programming in the early 90's long long ago. Cheers.