Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #112037

Re: TypeError: '_TemporaryFileWrapper' object is not an iterator

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: TypeError: '_TemporaryFileWrapper' object is not an iterator
Date 2016-07-30 01:17 +0000
Message-ID <mailman.39.1469841485.6033.python-list@python.org> (permalink)
References <5798BECB.8030800@rece.vub.ac.be> <579B1720.9000103@rece.vub.ac.be> <CACL+1atRTLUVKsJ3minLQB8hX=91z+BoA9sQPzFFB=pKmF+Bow@mail.gmail.com> <nngb4b$rus$1@ger.gmane.org> <CACL+1atsBtLNdifUPuo3E=gbb_hOUiiUT7Vpmv0UppQ4UeGhCA@mail.gmail.com>

Show all headers | View raw


On Fri, Jul 29, 2016 at 7:34 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> On 7/29/2016 7:59 AM, eryk sun wrote:
>>
>> On Fri, Jul 29, 2016 at 8:43 AM, Antoon Pardon
>> <antoon.pardon@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.

Yes, using a generator function is a simple way to create multiple
iterators of an iterable, which is especially useful for a sequence
that's independently reiterable. However a Python io file is its own
iterator because it's based on a synchronous operating system File
that maintains a file pointer. (Python io files don't support Windows
asynchronous file access, which doesn't update the File pointer, and
even the low-level _winapi and _overlapped implementations don't
support this.)

In this case there's no point that I can see in using a generator
function for __iter__. If you call iter() again on the
tempfile._TemporaryFileWrapper, it doesn't produce a new iteration of
the underlying file until seek(0) is called to reset the file pointer.
So __iter__ may as well  return `self` and have __next__ proxy
next(self.file).

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: TypeError: '_TemporaryFileWrapper' object is not an iterator eryk sun <eryksun@gmail.com> - 2016-07-30 01:17 +0000

csiph-web