Why is there no __iter__() for lists strings and tuples?
Jeff Epler
jepler at unpythonic.net
Fri Dec 20 16:36:11 EST 2002
On Fri, Dec 20, 2002 at 01:04:32PM -0800, Dennis Lee Bieber wrote:
> Maybe in Python 2.3 that difference will be unified? <G>
Seems like it needs to be.
class RandomString(str):
def __iter__(self):
i = str.__iter__(self)
while 1:
if random() < .5: i.next()
yield i.next()
>>> [c for c in RandomString("abcd")]
['c', 'd']
>>> [c for c in RandomString("abcd")]
['a', 'c']
>>> [c for c in RandomString("abcd")]
['a', 'c', 'd']
>>> # etc
Okay, so it's a pretty dumb use. But I'm sure there's a legitimate
reason to subclass str to modify __iter__ in terms of the underlying
string's iterator...
Jeff
More information about the Python-list
mailing list