[Python-3000] Builtin iterator type

Andrew Koenig ark-mlist at att.net
Sun Nov 19 17:56:31 CET 2006


> This looks like you're rediscovering or reinventing Java- or
> Zope-style interfaces: those are not passed on via inheritance but via
> a separate "implements" clause.

I don't think I'm quite reinventing Java interfaces.  In Java, where just
about everything is a method, it's temptingly easy to talk about the
interface to a type by making claims about its methods and the types they
return.  But in Python, such claims don't mesh well with the language's
dynamic nature.

So what I'm really looking for is a formal way of claiming that a type has
an informal property, and leaving it up to the programmers (and their unit
tests) to enforce the correspondence between the claims and the properties.

For iterators, for example, it's not just the presence of the __iter__ and
next methods that are relevant; it's also the notion that calling next can
raise StopIteration in the ordinary course of events, but if it raises any
other exception, it should be considered to be truly exceptional.  I don't
know how to capture such specifications formally, but I do think it would be
useful for a programmer to have a formal way of claiming that a given class
meets a given specification.


> I don't remember how it's done in Java
> (and don't have time to look it up) but it makes sense to me to use
> something akin to inheritance for constructing interfaces out of other
> interfaces, like your example of comparable inheriting from
> equality-comparable. But "being an interface" is not transmitted
> through this same mechanism -- that's a meta-property. I think that it
> would be a mistake to us isinstance to test for an interface.

Yes -- that's it exactly!

> I realize that Java- and Zope-style interfaces *seem* to be all about
> syntax (they define names and signatures of methods but not semantics)
> but IMO that's only a theoretical objection; in *practice* these
> interfaces have strong connotations of semantics (albeit written in
> Enlish rather than using assertions or pre- and post-conditions) and
> nobody would think of claiming to implement an interface without
> implementing the proper semantics (or some interpretation thereof :-).

I agree almost 100%.  Unfortunately, some people claim to implement
interfaces without appearing to think at all; but that's another subject
entirely :-)




More information about the Python-3000 mailing list