How to call this right? (polymorphism independent from inheritance)

Roman Suzi rnd at onego.ru
Wed May 2 04:03:24 EDT 2001


Hello!

The example below shows that polymorphism could be independent from
inheritance. (And some people think it is true OOP approach (and multiple
inheritance is wrong one), but it is underestimated in other langauges:
Java, C++). The method is very powerful, because it doesn't need stupid
abstract classes for every combination of attributes, and models the real
world of objects more purely.

Python supports this approach right out of the box
(or so it seems).

My questions are:

1. How to call it scientifically?
2. Will the code below really legal in __future__
3. Do you think I am right in the text above?

-------------------------------------------------------------------
def do_fly(self):
  return "I am flying."
def do_swim(self):
  return "I am swiming."
def do_sing(self):
  return "I am singing."

class Everybody:
  def i_am(self):
    return "I am %s." % self.__class__.__name__
  def __getattr__(self, attname):
    """This is added only for the text below to be "nice",
    in reality it is not needed"""
    def cant_do(self=None, action=attname):
      return "I can't %s." % action
    return cant_do

class Fish(Everybody):
  swim = do_swim

class Mermaid(Everybody):
  sing = do_sing
  swim = do_swim

class Bird(Everybody):
  fly = do_fly
  sing = do_sing

class Man(Everybody):
  sing = do_sing

# Examples of usage:

f = Fish(); r = Mermaid(); b = Bird(); m = Man()
print f.i_am(), f.swim(), f.sing(), f.fly()
print r.i_am(), r.swim(), r.sing(), r.fly()
print b.i_am(), b.swim(), b.sing(), b.fly()
print m.i_am(), m.swim(), m.sing(), m.fly()
print "Man learned to swim."
Man.swim = do_swim
print m.i_am(), m.swim(), m.sing(), m.fly()
---------------------------------------------------------------------

Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Wednesday, May 02, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "Cats have nine lives - but sleep through eight of them." _/





More information about the Python-list mailing list