TypeError: '_TemporaryFileWrapper' object is not an iterator

Terry Reedy tjreedy at udel.edu
Fri Jul 29 15:34:35 EDT 2016


On 7/29/2016 7:59 AM, eryk sun wrote:
> On Fri, Jul 29, 2016 at 8:43 AM, Antoon Pardon
> <antoon.pardon at rece.vub.ac.be> wrote:
>>
>> The problem seems to come from my expectation that a file
>> is its own iterator and in python3 that is no longer true
>> for a NamedTemporaryFile.
>
> For some reason it uses a generator function for __iter__

This is a common idiom for having iterable.__iter__ return an iterator 
without writing and instanciating an iterator class.

> instead of
 > returning self, which would allow it to proxy the wrapped file's
> __next__ method. There may be a good reason for that design decision,
> but at the moment I don't see it,

For statements, usually the preferred way to iterator through an 
iterable, always call iter() on the iterable, before calling next on the 
result.  Collections are generally written as iterables.  If one is 
going to explicitly call next, then one should also call iter explicity.

> so I'd simply try the following:

Not nearly as simple as either calling iter() or using a simple for loop 
with a one-line body.  (See response to Antoon.)

[snip complex solution]

-- 
Terry Jan Reedy




More information about the Python-list mailing list