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


Groups > comp.lang.python > #16013 > unrolled thread

Re: decorators and closures

Started byDave Angel <d@davea.name>
First post2011-11-21 10:06 -0500
Last post2011-11-21 10:06 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: decorators and closures Dave Angel <d@davea.name> - 2011-11-21 10:06 -0500

#16013 — Re: decorators and closures

FromDave Angel <d@davea.name>
Date2011-11-21 10:06 -0500
SubjectRe: decorators and closures
Message-ID<mailman.2904.1321887989.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web