Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57941 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2013-10-29 17:42 +0000 |
| Last post | 2013-10-29 17:42 +0000 |
| 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.
Re: stacked decorators and consolidating MRAB <python@mrabarnett.plus.com> - 2013-10-29 17:42 +0000
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2013-10-29 17:42 +0000 |
| Subject | Re: stacked decorators and consolidating |
| Message-ID | <mailman.1766.1383068572.18130.python-list@python.org> |
On 29/10/2013 16:54, 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)))
>
> 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?
>
If you apply the stacked decorators you get:
myfun = dec1(args1)(dec2(args2)(dec3(args3)(myfun)))
If you apply dec_all you get:
myfun = dec1(args1)(dec2(args2)(dec3(args3)))(myfun)
See the difference? You need the lambda to fix that.
Back to top | Article view | comp.lang.python
csiph-web