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


Groups > comp.lang.python > #57941

Re: stacked decorators and consolidating

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'tests.': 0.07; 'mess': 0.09; 'messing': 0.09; 'subject:skip:c 10': 0.09; 'def': 0.12; 'consolidate': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'lambda': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'preexisting': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'decorators': 0.24; 'passes': 0.24; 'fine': 0.24; "i've": 0.25; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'tim': 0.29; 'that.': 0.31; 'chase': 0.31; 'yields': 0.31; 'quite': 0.32; 'could': 0.34; 'something': 0.35; 'received:84': 0.35; 'sequence': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'results.': 0.60; 'different': 0.65; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'around,': 0.84; 'reply-to:addr:python.org': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=PIY2p5aC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=05MOGALpfCEA:10 a=aHz5RT5FWpoA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=zsEN09S7SQIA:10 a=g47lRzkD8ID2y8H7CVkA:9 a=wPNLvfGTeEIA:10
X-AUTH mrabarnett:2500
Date Tue, 29 Oct 2013 17:42:45 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: stacked decorators and consolidating
References <20131029115427.537e1bcd@bigbox.christie.dr>
In-Reply-To <20131029115427.537e1bcd@bigbox.christie.dr>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1766.1383068572.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383068572 news.xs4all.nl 15928 [2001:888:2000:d::a6]:38174
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57941

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: stacked decorators and consolidating MRAB <python@mrabarnett.plus.com> - 2013-10-29 17:42 +0000

csiph-web