Re: decorators and closures

2011-11-22 Thread 88888 Dihedral
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 > > d

Re: decorators and closures

2011-11-21 Thread Steven D'Aprano
On Mon, 21 Nov 2011 14:44:34 +, Andrea Crotti wrote: > With one colleague I discovered that the decorator code is always > executed, every time I call a nested function: "def" is a statement which is executed at runtime. Often people will talk about "definition time" instead of "compile time

Re: decorators and closures

2011-11-21 Thread Andrea Crotti
On 11/21/2011 05:11 PM, Dave Angel wrote: You didn't mention what version of Python you're running. With Python 2, I got very different results. So I switched to Python 3.2, and I still don't get exactly what you have. A closure is needed if there's some non-global data outside the functi

Re: decorators and closures

2011-11-21 Thread Dave Angel
On 11/21/2011 10:35 AM, Andrea Crotti wrote: On 11/21/2011 03:06 PM, Dave Angel wrote: 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 incompa

Re: decorators and closures

2011-11-21 Thread Andrea Crotti
On 11/21/2011 03:06 PM, Dave Angel wrote: 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 que

Re: decorators and closures

2011-11-21 Thread Dave Angel
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

decorators and closures

2011-11-21 Thread Andrea Crotti
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 dec