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


Groups > comp.lang.python > #72893

Re: Decorating one method of a class C with another method of class C?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'attribute': 0.07; 'class,': 0.07; 'dan': 0.09; 'executed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'def': 0.12; 'jan': 0.12; 'namespace,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'later': 0.20; 'seems': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'space.': 0.24; 'specify': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'apparently': 0.31; 'class': 0.32; 'becomes': 0.33; 'subject:with': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'subject:one': 0.36; 'method': 0.36; 'subject:?': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'received:71': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'new': 0.61; 'constructed.': 0.84; 'received:fios.verizon.net': 0.84; 'thereof': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Decorating one method of a class C with another method of class C?
Date Fri, 06 Jun 2014 21:34:09 -0400
References <CAGGBd_pM5DGp85LVfeQgYwqS8DU8nuQL8Q8G5EzfQ5BX26fa-w@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-71-175-90-87.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
In-Reply-To <CAGGBd_pM5DGp85LVfeQgYwqS8DU8nuQL8Q8G5EzfQ5BX26fa-w@mail.gmail.com>
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.10839.1402104871.18130.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402104871 news.xs4all.nl 2902 [2001:888:2000:d::a6]:49085
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72893

Show key headers only | View raw


On 6/6/2014 8:14 PM, Dan Stromberg wrote:
> Is there a way of decorating method1 of class C using method2 of class C?
>
> It seems like there's a chicken-and-the-egg problem; the class doesn't
> seem to know what "self" is until later in execution so there's
> apparently no way to specify @self.method2 when def'ing method1.

Class code is executed in a new local namespace, which later becomes the 
class attribute space. So you can (sort of), but I cannot see a reason 
to do so.

class C:
     def deco(f):
         print('decorating')
         def inner(self):
             print('f called')
             return f(self)
         return inner

     @deco
     def f(self):
         print(self)

     del deco  # because it is not really a method
     # but rather an undecorated static method

c = C()
c.f()
 >>>
decorating
f called
<__main__.C object at 0x000000000348B898>

If deco is decorated as a staticmethod, it has to be called on the class 
or an instance thereof after the class is constructed. If deco is 
defined outside the class, it can be used both inside and outside.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Decorating one method of a class C with another method of class C? Terry Reedy <tjreedy@udel.edu> - 2014-06-06 21:34 -0400

csiph-web