[Python-3000] Fwd: Re: PEP: rename it.next() to it.__next__(), add a next() built-in

Raymond Hettinger python at rcn.com
Mon Mar 5 23:46:39 CET 2007


[Raymond]
>> My thought here is that iterators should always be a separate object -- there is no good reason for dir(iter(myfile)) to expose methods that have nothing to do with iteration.  In the case of files, it would not be hard to have a singleton file-iterator object.

[GvR]
>The question isn't how hard it would be, but whether it would be the
>right thing to do. For iterators where (by nature of the object being
>iterated over) there's only one iterator possible, I think returning
>self is the right thing to do, as it makes it abundantly clear that
>multiple parallel iterations are unsupported.

I don't follow how returning self makes anything clear -- the only way to know that self was returned is to check object ids or to guess why dir() on the iterator returns all of the file methods:


>>> f = open('tmp.txt')
>>> it1 = iter(f)
>>> it2 = iter(f)
>>> map(id, [f, it1, it2])    # only this check shows that 'iter(f) is f'
[3083536096L, 3083536096L, 3083536096L]
>>> dir(it1)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close', 'closed', 'encoding', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']

ISTM, we might as well make the singletonness explicit and hide the file methods from the iterator object (nothing good can come from conflating file ops with iteration).


Raymond


More information about the Python-3000 mailing list