Inheritance Question

Jackson jackson at hotmail.com
Fri Nov 10 19:13:53 EST 2006


I've got an inheritance question and was hoping brighter minds could
guide me.  I am in the strange situation where some of the methods in a
subclass are actually more general than methods in a superclass.  What
is the preferred way to handle such situations.  My original thought was
to do something like this:

class AA(object):
	def general_method(): pass

class A(AA):
   # redefine general_method() to call a
   # restricted version of AA.general_method()

class B(A,AA):
   # redefine general_method() to call AA.general_method()

This seems ugly to me, and I am wondering if there is a better method.
So any suggestions would be appreciated.


Thanks!






-----------

For a more "concrete" example:

Suppose all the animals in the world have only 1 or 2 legs.
	
class Legs(object)
  def run(): pass
  def walk(number_of_legs):
    # lots of commands
    # that do not depend on the
    # number of legs but definitely
    # have to do with walking

    if number_of_legs == '1':
       # blah blah

    if number_of_legs == '2':
       # blah blah

    # more commands

class HasAtLeastOneLeg(Legs):
  def walk():
    # Legs.walk(number_of_legs=1)

class HasTwoLegs(HasAtLeastOneLeg,Legs):
  def walk()
    # Legs.walk(number_of_legs=2)

# isinstance(HasTwoLegs, HasAtLeastOneLeg) --> True



More information about the Python-list mailing list