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


Groups > comp.lang.python > #57945

Re: stacked decorators and consolidating

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'tests.': 0.07; 'decorator': 0.09; 'mess': 0.09; 'messing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'def': 0.12; '-tkc': 0.16; 'consolidate': 0.16; 'help?': 0.16; 'lambda': 0.16; 'messy': 0.16; 'preexisting': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; '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:X-Complaints-To:1': 0.27; 'function': 0.29; 'tim': 0.29; 'chase': 0.31; 'yields': 0.31; 'quite': 0.32; 'could': 0.34; 'something': 0.35; 'sequence': 0.36; 'should': 0.36; 'to:addr :python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'results.': 0.60; 'first': 0.61; 'times': 0.62; 'different': 0.65; 'around,': 0.84; 'decorate': 0.84; 'doubts': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: stacked decorators and consolidating
Date Tue, 29 Oct 2013 18:52:19 +0100
Organization None
References <20131029115427.537e1bcd@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849636.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
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.1768.1383069113.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383069114 news.xs4all.nl 15890 [2001:888:2000:d::a6]:48603
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57945

Show key headers only | View raw


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


Thread

Re: stacked decorators and consolidating Peter Otten <__peter__@web.de> - 2013-10-29 18:52 +0100

csiph-web