[Python-3000] Making strings non-iterable

Delaney, Timothy (Tim) tdelaney at avaya.com
Tue Apr 18 06:09:34 CEST 2006


Barry Warsaw wrote:

> I wouldn't want to generalize this, but it is not infrequent that
> people mistakenly iterate over strings when they want to treat them
> atomically. difflib not withstanding, and keeping Guido's
> pronouncement in mind, I do think people want to treat strings
> atomically much more often then they want to treat them as a sequence
> of characters. 

Perhaps we need a "no really - I really want to iterate over this thing"
built-in and magic method. Then things that should generally be treated
atomically would not implement __iter__, but could implement
__deep_iter__ (which would return a standard iterator).

This would allow both use cases, but atomicity being the default (i.e. a
for loop continues to use __iter__).

    for e in deep_iter(obj):
        print e

Tim Delaney


More information about the Python-3000 mailing list