[Python-3000] Builtin iterator type

George Sakkis george.sakkis at gmail.com
Thu Nov 16 03:49:12 CET 2006


On 11/15/06, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> George Sakkis wrote:
>
> > class Container(object):
> >
> > class Sequence(Container):
>
> > class Mapping(Container):
> >
> > Note that this does _not_ disallow duck typing; anyone is perfectly
> > free to define a method len() in a class that does not extend any of
> > the above so that a client can call x.len() successfully.
>
> What's the point, then? What benefit do I get from
> extending Sequence if I can achieve the same thing
> without bothering?

For one thing, the implementations you get for free for the methods
that can be implemented without needing to know the specifics of the
concrete class (think DictMixin, ListMixin and friends; all these
would move there). So yes, for the nitpickers out there, these are not
interfaces in the Java sense (or pure virtual classes in C++) but
abstract classes.

And for two, not everyone feels comfortable with duck typing. People
who consider (for better or for worse) isinstance() safer than
hasattr()/getattr() would be accomodated too. Why do we have to
alienate those folks telling them they are wrong when both approaches
can coexist, as it happens for LBYL vs EAFP error strategies,
procedural vs OO vs functional paradigms, and other dichotomies and
n-otomies that Python has successfully brought together ?

George


More information about the Python-3000 mailing list