Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16012
| Date | 2011-11-21 14:44 +0000 |
|---|---|
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
| Subject | decorators and closures |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2903.1321886678.27778.python-list@python.org> (permalink) |
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 | Next — Next in thread | Find similar | Unroll 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