Missing interfaces in Python...
Terry Reedy
tjreedy at udel.edu
Mon Apr 17 19:39:00 EDT 2006
<redefined.horizons at gmail.com> wrote in message
news:1145306373.781129.305840 at z34g2000cwc.googlegroups.com...
> I'm coming from a Java background, so please don't stone me...
Most of us came to Python from some other language background ;-)
> I see that Python is missing "interfaces".
As someone else noted, Python objectively does not have 'interfaces' (or
'protocols') as an object type in the language. (But 'missing' is somewhat
subjective.) On the other hand, the concepts are very much part of the
language. See the article on duck typing, which could be called duck
interfacing, that someone gave a link for.
For example, an iterator (newer definition) is an object with an __iter__()
method returning self and a next() method that returns objects until it
raises StopIteration. An iterable is an object with an __iter__() method
that return an iterator. (Hence, iterators are conveniently iterables
also.) Instead of declaring that a class implements IterableInterface, you
just implement it (and the corresponding iterator class if needed) and use
it anywhere an iterable is expected, which is lots places in the builtins
and standard library.
> How would I approach this problem in Python? I think I would use an
> abstract class instead of an interface for IFixable, since Python
> supports multiple inheritance, but I'm not sure this is correct.
I believe this
>>> NotImplementedError
<class exceptions.NotImplementedError at 0x00974810>
was added so that people could go the route of abstract base classes with
stub functions.
Terry Jan Reedy
More information about the Python-list
mailing list