Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57945
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: stacked decorators and consolidating |
| Date | 2013-10-29 18:52 +0100 |
| Organization | None |
| References | <20131029115427.537e1bcd@bigbox.christie.dr> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1768.1383069113.18130.python-list@python.org> (permalink) |
Tim Chase wrote: > I've got some decorators that work fine as such: > > @dec1(args1) > @dec2(args2) > @dec3(args3) > def myfun(...): > pass > > However, I used that sequence quite a bit, so I figured I could do > something like > > dec_all = dec1(args1)(dec2(args2)(dec3(args3))) With these shortcuts a = dec1(args1) b = dec2(args2) c = dec3(args3) to make it look less messy your first attempt is dec_all = a(b(c)) and the final decorated function will be a(b(c))(myfun) when it should be a(b(c(myfun))) i. e. instead of decorating myfun three times you are decorating the decorator c twice and then use the result of that decoration to decorate myfunc. Does that help? I have my doubts ;) > to consolidate the whole mess down to > > @dec_all > def myfun(...): > pass > > However, this yields different (test-breaking) results. Messing > around, I found that if I write it as > > dec_all = lambda fn: dec1(args1)(dec2(args2)(dec3(args3)(fn))) > > it works and passes all preexisting tests. > > What am I missing that would cause this difference in behavior? > > -tkc
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: stacked decorators and consolidating Peter Otten <__peter__@web.de> - 2013-10-29 18:52 +0100
csiph-web