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


Groups > comp.lang.python > #16012

decorators and closures

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 <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'interpreter': 0.05; 'decorator': 0.09; 'function:': 0.09; 'def': 0.13; 'betting': 0.16; 'executed,': 0.16; 'code': 0.25; "i'm": 0.26; 'message- id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'to:addr :python-list': 0.34; 'possible,': 0.34; 'nested': 0.34; 'but': 0.37; 'received:google.com': 0.37; 'received:10.0.0': 0.38; 'received:209.85': 0.38; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'discovered': 0.68
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:subject :content-type:content-transfer-encoding; bh=r6rt9SOC5XV9jrRGLi51pF+Km/RP1R4MPTRH7IOLtFM=; b=eBADl2nL85DgnHsc++Q20GmZw/PIbz9ED6jtetaBLLMgprBNhepDvQHh5jUxpaXMUx 0j5nwEj7G+kYVFcYbP1jZmXu93+W3kUpDJ020Ev/3MWYciAJVDAoSxfC4Dw7j9k20d2p XIPutOFpGWNH0IHmJrUAxERvIuALBV/2IkfR4=
Date Mon, 21 Nov 2011 14:44:34 +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 python-list@python.org
Subject decorators and closures
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.2903.1321886678.27778.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1321886678 news.xs4all.nl 6890 [2001:888:2000:d::a6]:52094
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16012

Show key headers only | View raw


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 :)

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


Thread

decorators and closures Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-11-21 14:44 +0000
  Re: decorators and closures Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-22 03:54 +0000
  Re: decorators and closures 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-22 09:03 -0800
  Re: decorators and closures 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-22 09:03 -0800

csiph-web