Am I missing something with Python not having interfaces?

hdante hdante at gmail.com
Tue May 6 10:17:15 EDT 2008


On May 6, 10:44 am, jmDesktop <needin4mat... at gmail.com> wrote:
> Studying OOP and noticed that Python does not have Interfaces.  Is
> that correct?  Is my schooling for nought on these OOP concepts if I
> use Python.  Am I losing something if I don't use the "typical" oop
> constructs found in other languages (Java, C# come to mind.)  I'm
> afraid that if I never use them I'll lose them and when I need them
> for something beside Python, I'll be lost.  Thank you.

 Python supports interfaces. In the example below, "Vehicle" is an
interface.

class Vehicle:
     def drive(self, count): raise Exception("I'm only an
interface... :-(")
     def number_of_wheels(self): return 0
     def fly(self): pass

class Car(Vehicle):
     def drive(self, count): print "The car walked %d steps" % count
     def number_of_wheels(self): return 4

 As you can see, there are a couple of ways you can tell others
"Vehicle" is an interface, like raising exceptions, returning useless
values or doing nothing. You could also raise an exception in
Vehicle.__init__.




More information about the Python-list mailing list