OO in Python? ^^

Brian Beck exogen at gmail.com
Sat Dec 10 18:18:00 EST 2005


Matthias Kaeppler wrote:
> class Base:
>    def foo(self): # I'd like to say that children must implement foo
>       pass

def foo(self):
     raise NotImplementedError("Subclasses must implement foo")

Now calling foo on a child instance will fail if it hasn't implemented foo.

> And how do I formulate polymorphism in Python? Example:
> 
> class D1(Base):
>    def foo(self):
>       print "D1"
> 
> class D2(Base):
>    def foo(self):
>       print "D2"
> obj = Base() # I want a base class reference which is polymorphic
> if (<need D1>):
>    obj =  D1()
> else:
>    obj = D2()

I have no idea what you're trying to do here and how it relates to 
polymorphism.

-- 
Brian Beck
Adventurer of the First Order



More information about the Python-list mailing list