General question about Python design goals

Fredrik Lundh fredrik at pythonware.com
Thu Dec 1 15:49:47 EST 2005


Mike Meyer wrote:

>>> Seriously. Why doesn't this have to be phrased as "for x in list((1,
>>> 2, 3))", just like you have to write list((1, 2, 3)).count(1), etc.?
>> because anything that supports [] can be iterated over.
>
> That's false. Anything that has __getitem__ supports []. To be
> iterated over, it has to have __iter__, or an __getitem__ that works
> on integers properly.

your arguments are getting more and more antoon-like.  you can create
an iterator (using iter) for any object that supports [] (__getitem__), but
if __getitem__ doesn't do the right thing, any attempt to fetch items by
calling it will of course fail as well.

> Writing a class that meets the first without
> meeting the second is easy. Dicts used to qualify, and tuples could be
> iterated over at that time.
>
> Not really very satisfactory reasons.

that's how python's sequence protocol works.  duck typing all the
way down.

    http://www.google.com/search?q=duck+typing

</F> 






More information about the Python-list mailing list