Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!rt.uk.eu.org!news-transit.tcx.org.uk!newsfeed.xs4all.nl!newsfeed5.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; ':-)': 0.06; 'question:': 0.07; 'differing': 0.09; 'foo': 0.09; 'problem:': 0.09; 'subclass': 0.09; 'subclasses': 0.09; 'subject:method': 0.09; 'anyway': 0.09; 'def': 0.13; '"calling': 0.16; ':o)': 0.16; 'answer,': 0.16; 'explanation': 0.16; 'instance)': 0.16; 'problem).': 0.16; 'subclassing': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'instance': 0.18; 'cc:no real name:2**0': 0.20; 'asked': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'suspect': 0.24; 'code': 0.25; 'performing': 0.26; '(in': 0.26; 'raise': 0.28; '(and': 0.28; '(even': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'error': 0.29; 'class': 0.29; 'subject:?': 0.31; 'thanks': 0.31; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'there': 0.33; 'probably': 0.34; 'too': 0.34; 'approach.': 0.34; 'explain': 0.36; 'question': 0.36; 'acceptable': 0.37; 'class.': 0.37; 'but': 0.37; 'subject:can': 0.37; 'doing': 0.38; 'some': 0.38; 'being': 0.39; 'why': 0.39; 'called': 0.40; 'more': 0.61; 'design': 0.61; 'custom': 0.61; 'waiting': 0.63; 'here': 0.65; 'alternative': 0.65; 'trial': 0.68; 'redesign': 0.84; 'discovering': 0.91; 'approached': 0.97 X-IronPort-AV: E=Sophos;i="4.71,461,1320620400"; d="scan'208";a="48773" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Thu, 05 Jan 2012 12:09:41 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: Peter Subject: Re: can a subclass method determine if called by superclass? References: <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com> In-Reply-To: <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325761851 news.xs4all.nl 6923 [2001:888:2000:d::a6]:32797 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18546 Peter wrote: > Situation: I am subclassing a class which has methods that call other > class methods (and without reading the code of the superclass I am > discovering these by trial and error as I build the subclass - this is > probably why I may have approached the problem from the wrong > viewpoint :-)). > > Problem: when overriding one of these "indirectly called" superclass > methods I would like to take differing actions (in the subclass > instance) depending on whether it is the superclass or the subclass > instance performing the call. > > Question: Is there any way to determine in a method whether it is > being called by the superclass or by a method of the subclass > instance? > > Now I suspect that what I am doing is actually very muddy thinking :-) > and I don't want to attempt to explain why I am approaching the design > this way as an explanation would require too much work - I will > consider an alternative inheritance approach while waiting an answer, > but the answer to the question interested me (even if I do a redesign > and come up with a more "elegant" approach to the problem). > > Thanks > Peter > As you suspected, this is probably the wrong approach. However since you asked for a solution anyway :o) class Parent(object): def foo(self): # implementation by subclasses is still REQUIRED if self.__class__ is Parent: raise NotImplementedError() # common code for all foo methods print "calling foo" class Child(Parent): def foo(self): # You can still call the virtual method which contains some code Parent.foo(self) # here the custom code p = Parent() c = Child() c.foo() p.foo() Note that this is not the best approach, still acceptable because there is no code specific to a subclass in the base class. JM