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


Groups > comp.lang.python > #18523

Re: can a subclass method determine if called by superclass?

References <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-01-04 16:09 -0700
Subject Re: can a subclass method determine if called by superclass?
Newsgroups comp.lang.python
Message-ID <mailman.4433.1325718597.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jan 4, 2012 at 3:42 PM, Peter <peter.milliken@gmail.com> 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?

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


Thread

can a subclass method determine if called by superclass? Peter <peter.milliken@gmail.com> - 2012-01-04 14:42 -0800
  Re: can a subclass method determine if called by superclass? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-04 16:09 -0700
    Re: can a subclass method determine if called by superclass? Peter <peter.milliken@gmail.com> - 2012-01-04 15:37 -0800
      Re: can a subclass method determine if called by superclass? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-05 02:18 +0000
  Re: can a subclass method determine if called by superclass? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-01-05 12:09 +0100
  Re: can a subclass method determine if called by superclass? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-01-05 12:13 +0100
    Re: can a subclass method determine if called by superclass? Peter <peter.milliken@gmail.com> - 2012-01-05 10:59 -0800

csiph-web