string: __iter__()?

mrquantum mrquantum at holon.at
Wed Oct 4 19:45:03 CEST 2006


John Roth:
 
> The iter() builtin creates an iterator for any
> object that obeys the sequence protocol.
> Since strings obey the sequence protocol there
> is no real advantage to adding yet another
> protocol to an already very fat object.

Okay!

> This  does, however, mean that testing
> for the presence of __iter__ is incomplete;
> one also has to test for __getattr__ if the
> object doesn't have an __Iter__ method.

Should be __getitem__ and not __getattr__!?

> Depending on  your program logic, it
> may be easier to just use iter() and
> handle the exception if it fails.

Okay, I'll do it this way - except will then raise a TypeError, as I just
found in the docs!

> See PEP 234 for a discussion of the
> reasons for doing it this way.
> 

Thanks for pointing this out!

Chris

PS: Thanks to all posters in this thread for your illuminative comments!



More information about the Python-list mailing list