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


Groups > comp.lang.python > #16080

Re: decorators and closures

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.python
Subject Re: decorators and closures
Date 2011-11-22 09:03 -0800
Organization http://groups.google.com
Message-ID <24059779.511.1321981422154.JavaMail.geo-discussion-forums@prou19> (permalink)
References <mailman.2903.1321886678.27778.python-list@python.org>

Show all headers | View raw


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.   

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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