Am I missing something with Python not having interfaces?

Matt Nordhoff mnordhoff at mattnordhoff.com
Tue May 6 15:00:22 EDT 2008


Mike Driscoll wrote:
> On May 6, 8: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.
> 
> In my school, we didn't even discuss the concept of interfaces (except
> for CLI and GUI, that is). So I looked it up. I assume you are
> referring to something like what's found here:
> 
> http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
> 
> If so, then it looks like an Interface is a generic class with method
> stubs. You can do that with Python just as easily by creating empty
> methods with just the "pass" keyword. It also reminds me of
> Decorators...so you might want to look at those.

Instead of just using "pass", you should "raise NotImplementedError".

>From the docs: "In user defined base classes, abstract methods should
raise this exception when they require derived classes to override the
method."

> Since it's just a construct to implement polymorphism, I don't think
> you'll lose anything. However, Python does not require you to re-
> implement every method of the class it is inheriting from. You can
> just override those that you want and leave the others alone.
> 
> Hopefully I understand this correctly...otherwise, just ignore my
> babbling and hand waving.
> 
> Mike
-- 



More information about the Python-list mailing list