OO in Python? ^^

Matthias Kaeppler void at void.com
Sat Dec 10 17:55:18 EST 2005


Hi,

sorry for my ignorance, but after reading the Python tutorial on 
python.org, I'm sort of, well surprised about the lack of OOP 
capabilities in python. Honestly, I don't even see the point at all of 
how OO actually works in Python.

For one, is there any good reason why I should ever inherit from a 
class? ^^ There is no functionality to check if a subclass correctly 
implements an inherited interface and polymorphism seems to be missing 
in Python as well. I kind of can't imagine in which circumstances 
inheritance in Python helps. For example:

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

class Child(Base):
    pass # works

Does inheritance in Python boil down to a mere code sharing?

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 could as well leave the whole inheritance stuff out and the program 
would still work (?).

Please give me hope that Python is still worth learning :-/

Regards,
Matthias



More information about the Python-list mailing list