Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'question:': 0.07; 'differing': 0.09; 'problem:': 0.09; 'subclass': 0.09; 'subject:method': 0.09; 'filename.': 0.16; 'instance)': 0.16; 'subclassing': 0.16; 'such?': 0.16; 'cc:addr:python-list': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'instance': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'uses.': 0.23; 'cc:2**0': 0.24; 'received:74.125.82.174': 0.24; 'stack': 0.24; 'code': 0.25; 'performing': 0.26; '(in': 0.26; '(and': 0.28; 'message-id:@mail.gmail.com': 0.28; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'error': 0.29; 'pm,': 0.29; 'class': 0.29; 'subject:?': 0.31; 'implement': 0.32; 'actually': 0.33; 'there': 0.33; 'probably': 0.34; 'received:74.125.82': 0.35; 'two': 0.37; 'but': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'subject:can': 0.37; 'could': 0.37; 'using': 0.38; 'being': 0.39; 'why': 0.39; 'called': 0.40; '2012': 0.67; 'trial': 0.68; 'discovering': 0.91; 'approached': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=zupKjLT/Z3mx1b/WaVNULnoy38V+v5w1Vb3ZZmPchTg=; b=Aon333ZsPuH2SHLb7uiAFJsbvPcKW5UZrMlKCBZ0NtfO0GpOEIbKkFZfLJYkwORciR YWt4H8t6rMcF13u64CbyFN3Q0btYqt+BHEq7Nf+uscCOvuMSsN7ZDGhV6HgM8FwSKMhb RA97bJI5tu5dQ0T8h43glX4W97z4vl0btx9t0= MIME-Version: 1.0 In-Reply-To: <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com> References: <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com> From: Ian Kelly Date: Wed, 4 Jan 2012 16:09:24 -0700 Subject: Re: can a subclass method determine if called by superclass? To: Peter Content-Type: text/plain; charset=ISO-8859-1 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325718597 news.xs4all.nl 6976 [2001:888:2000:d::a6]:37160 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18523 On Wed, Jan 4, 2012 at 3:42 PM, 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? Well, you could get the previous stack level using traceback.extract_stack() and check the filename. But it sounds like what you actually have are two different methods -- one that is used by the superclass, and one that only the subclass knows about and uses. Why not implement it as such?