Is a language without interfaces a bad choice for teaching OOP (Python)?

Perhaps I am dated on my information, but I was going to help someone learn an OOP language, concepts, etc. I wanted to use something dynamic. My thought was Python but then I read it has no Interfaces. Isn't an Interface something someone needs to know to learn OOP concepts?

Answers


My thought was Python but then I read it has no Interfaces.

Well, you can use interfaces in Python. The standard library has the abc module, and there are third-party modules like PyProtocols (and frameworks like Zope and Twisted have their own similar ideas).

The point is that they aren't required, and frequently aren't even necessary. You "wanted to use something dynamic"? That's what it means to be dynamic: Functions take any object with the right interface, without needing that interface to be statically defined anywhere, much less needing the object to statically declare that it supports the interface.

So, when you ask:

Isn't an Interface something someone needs to know to learn OOP concepts?

The answer is "No". An Interface is something someone needs to know to learn Java-style OOP, but it's not something someone needs to know to learn OOP in general.

Consider this from a different angle: It's possible, but not easy, to do Javascript-style OOP in Python; as in Java, classes are too fundamental to Python to live without. Does that make Python and Java bad OO languages and Javascript a good one? No; it just makes them different OO languages.


monkut wanted to know "wtf is an interface???"

An interface (aka protocol, abstract base class, abstract type, …) is just a type that can be used for static and/or dynamic type checking and/or switching in exactly same way a class can.

So, what's the good of that? Well, in theory, you should never need to inspect types—that's not just part of the Zen of Python, it's part of core OO dogma as well—but in practice sometimes you do. So, being able to name your abstract types, and declare that a class supports a variety of different abstract types, can be useful.

Of course you could do that with plain old classes, but being able to explicitly declare that a type is abstract can also help with readability and debugging. PEP 3119 explains the rationale in a Python-centric way.

But in languages like Java, there's are two additional benefits.

First, if you have static type checking, you can't write a single function that can take, e.g., a list, a tuple, a set, a frozenset, or an iterator. But you can write a function that takes an Iterable, and then declare that list, tuple, etc. all provide the Iterable interface, and then everything is fine. (In a language with dynamic type checking, duck typing already takes care of this for you—your code works with any object that has an __iter__ method that returns something that behaves the way you expect it to.)

Second, if you've got static data member layout and/or vtable-style method override mechanism, multiple inheritance is very tricky. But multiple inheritance is also very useful. So in Java, an interface is something just like a class, but with no data members or method implementations, and you can inherit from as many interfaces as you want, but only one class. This gives Java some of the benefits of multiple inheritance, without any of the problems. (In a language with dynamic data members and dynamic method lookup, as long as you have a sensible MRO algorithm, as Python does, you can get all of the benefits of multiple inheritance without any of the problems.)


Need Your Help

HTML form design useful links and reading

html html-form ui-design

I think most of us have seen the following:

SQL Alchemy - schema extraction with TypeDecorator

types sqlalchemy decorator database-schema getter

I am using a TypeDecorator for Json extraction and another model uses it for one of its columns. I am storing python list objects using this TypeDecorator.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.