How to define an Interface in Python 2.2.2?

Doug doug at doug-.com
Thu Apr 8 22:38:11 EDT 2004


Boo Yah wrote:
> Does Python 2.2.2 support Interface (as PHP 5 does) ?
> 
> If so, how can i define it in Py 2.2.2?

Python doesn't directly support interfaces.
What most people for an interface is to create a class where
the methods raise a NotImplementedError. For example:

class MyInterface:
     def method1():
         raise NotImplementedError
     def method2():
         raise NotImplementedError

class MyClass (MyInterface):
     "implements MyInterface"
     def method1():
         some code here...
     def method2():
         some code here...

But see this page for some attempts at creating interfaces
for Python: http://www.python.org/cgi-bin/moinmoin/MetaClasses



More information about the Python-list mailing list