Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18522 > unrolled thread
| Started by | Peter <peter.milliken@gmail.com> |
|---|---|
| First post | 2012-01-04 14:42 -0800 |
| Last post | 2012-01-05 10:59 -0800 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Peter <peter.milliken@gmail.com> |
|---|---|
| Date | 2012-01-04 14:42 -0800 |
| Subject | can a subclass method determine if called by superclass? |
| Message-ID | <13ac20bc-e8b0-4697-8dfd-9fa003af2a48@a17g2000yqj.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-01-04 16:09 -0700 |
| Message-ID | <mailman.4433.1325718597.27778.python-list@python.org> |
| In reply to | #18522 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Peter <peter.milliken@gmail.com> |
|---|---|
| Date | 2012-01-04 15:37 -0800 |
| Message-ID | <e1daeb80-70cd-42e9-a782-69ed595f55a2@a11g2000vbz.googlegroups.com> |
| In reply to | #18523 |
On Jan 5, 10:09 am, Ian Kelly <ian.g.ke...@gmail.com> wrote: > > 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? Thanks Ian - that is one possibility. I am trying to create a subclass with slightly different functionality and use it with an existing code base i.e. there is already one or more modules that instantiate the current superclass and I want to just drop in this new class to replace it with no ripples up the line (so to speak). The new class implements some interface changes that can safely be hidden from the rest of the application.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-01-05 02:18 +0000 |
| Message-ID | <4f05086c$0$29895$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #18526 |
On Wed, 04 Jan 2012 15:37:55 -0800, Peter wrote:
> I am trying to create a subclass with slightly different functionality
> and use it with an existing code base i.e. there is already one or
> more modules that instantiate the current superclass and I want to
> just drop in this new class to replace it with no ripples up the line
> (so to speak). The new class implements some interface changes that
> can safely be hidden from the rest of the application.
This is *exactly* the idea behind subclassing. I don't understand your
problem, can you explain more?
If you want to change behaviour of an object, you subclass it, then
override or overload the methods you want to change. You certainly
shouldn't be changing the superclass to recognise when it is being called
from a subclass! That's completely the wrong approach -- you should put
all the new behaviour in the new class.
# WRONG! Don't do this.
class Parent(object):
def method(self, arg):
if type(self) is not Parent:
# Must be a subclass.
print("Called from a subclass. But which one?")
print("Doing method stuff.")
class Child(Parent):
pass
# RIGHT! Do this instead.
class Parent(object):
def method(self, arg):
print("Doing method stuff.")
class Child(Parent):
def method(self, arg):
# Overload an existing method.
print("Called from Child subclass.")
super().method(arg) # Python 3 only
# Python 2 use: super(Child, self).method(arg)
If method() returns a result, you can capture the result of calling the
superclass method and then modify it as needed. Or you can override the
method completely, and not call the parent method() at all.
Now you can use Child() anywhere that you can use Parent and the caller
shouldn't even notice. Or at least that is the idea behind subclassing,
although it is possible to break it. You will be okay if the caller uses
duck-typing, or isinstance checks, but not if they do exact type checks
(which is almost always the wrong thing to do).
c = Child()
# Duck-typing works:
hasattr(c, 'method') # returns True
# So do isinstance checks:
isinstance(c, Parent) # returns True
# but this doesn't and defeats the purpose of having subclasses:
type(c) is Parent # returns False, you shouldn't do this!
If the caller does do exact type checks, then almost certainly it should
be considered a bug in their code and they should be beaten with a clue-
bat and told to use isinstance (good) or duck-typing (better still).
If I have misunderstood your problem, can you give a concrete (but
simple) example of what you are trying to do?
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2012-01-05 12:09 +0100 |
| Message-ID | <mailman.4447.1325761851.27778.python-list@python.org> |
| In reply to | #18522 |
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
[toc] | [prev] | [next] | [standalone]
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2012-01-05 12:13 +0100 |
| Message-ID | <mailman.4448.1325762028.27778.python-list@python.org> |
| In reply to | #18522 |
Jean-Michel Pichavant wrote: > 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 I just realized I didn't addressed the problem you described, sorry, just ignore my mail. JM
[toc] | [prev] | [next] | [standalone]
| From | Peter <peter.milliken@gmail.com> |
|---|---|
| Date | 2012-01-05 10:59 -0800 |
| Message-ID | <0d85f158-250d-429f-b8c2-e3c53c9d7ca8@d9g2000yqg.googlegroups.com> |
| In reply to | #18547 |
Thanks for the help guys - it appears there is no better way than Ian suggested. I have restructured my class hierarchy so the problem has now "gone away". It would have been nice to discover some previously unknown (to me at least!) Python trick, but such is life :-) Regards Peter
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web