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: 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 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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