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


Groups > comp.lang.python > #112037 > unrolled thread

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

Started byeryk sun <eryksun@gmail.com>
First post2016-07-30 01:17 +0000
Last post2016-07-30 01:17 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#112037 — Re: TypeError: '_TemporaryFileWrapper' object is not an iterator

Fromeryk sun <eryksun@gmail.com>
Date2016-07-30 01:17 +0000
SubjectRe: TypeError: '_TemporaryFileWrapper' object is not an iterator
Message-ID<mailman.39.1469841485.6033.python-list@python.org>
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).

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web