如何覆盖“for file in file:” [英] How to overide "for line in file:"
问题描述
如何在构造中暗示的迭代器覆盖文件中的行
:"?
例如,假设我有一个每个
行包含行,列对的文件,我希望编写一个文件的子类,它将使用仿射变换将这些
转换为x,y坐标。我希望它看起来像这样的东西(但这显然不起作用):
class myFile(file):
def __init __(self,name,affine):
self.affine = affine
file .__ init __(name)
>
def next(self):
for line in file .__ iter __(self):
r,c ="," .split(line [ :-1]
收益self.affine(r,c)
提前致谢!
dave
< David Morgenthaler>在留言中写道
news:ti ********** ********************** @ 4ax.com ...如何覆盖构造隐含的迭代器for文件中的行
:"?
例如,假设我有一个文件,每行包含行,列对,我希望编写一个文件的子类将使用仿射变换将这些
转换为x,y坐标。我希望它看起来像这(但这显然不起作用):
类myFile(文件):
def __init __(self,name,affine):
self.affine = affine < br。> file .__ init __(name)
def next(self):
for line in file .__ iter __(self):
r,c ="," .split(行[: - 1]
产生self.affine(r,c)
提前致谢!
我不确定你要做什么,但你可能需要
来寻找readline()方法。
John Roth
dave
David Morgenthaler写道:
如何覆盖构造中隐含的迭代器for for line
in file:?
''for''使用__iter __()。
子类文件并重新定义__iter __(self)。它应该返回一个对象,
有next()方法返回下一个项目,或者提出StopIteration
如果完成。从__iter __()返回self是可以的(然后你可以将
next()放在同一个类中)。
更简单的选择是make __iter__ ()一个生成器函数(所以调用
它会自动返回一个''generator''对象,它上面有next(),
也会在完成时引发StopIteration) 。
HTH,
Shalabh
" John罗斯" <是ne ******** @ jhrothjr.com>在消息中写道
news:10 ************* @ news.supernews.com ...
< David Morgenthaler>在消息中写道
新闻:ti ******************************** @ 4ax.com ... < blockquote class =post_quotes>如何覆盖构造中隐含的迭代器for for line
in file:?
包裹它,不要替换它。见下文。
例如,假设我有一个文件,每行包含行,列对,我希望编写一个文件的子类,将这些转换为使用仿射变换的x,y坐标。我希望它看起来像这样(但这显然不起作用):
你的班级需要一个__iter__功能。但是,除非你有重型级别选项的其他原因,否则就更容易了:
def xycoords(直线):
#lines是一个可迭代产生适当格式的文本行
for line in lines:
< extract numbers and transform>
收益率x,y
您可以为xycoords提供一个文字列表进行测试,然后打开一个
文件供生产使用。
像这样的链接迭代器是它们预期用途的一部分。
Terry J. Reedy
How does one overide the iterator implied by the construct "for line
in file:"?
For example, suppose I have a file containing row,col pairs on each
line, and I wish to write a subclass of file that will transform these
to x,y coordinates using an affine transform. I''d like it to look
something like this (but this clearly doesn''t work):
class myFile(file):
def __init__(self,name,affine):
self.affine = affine
file.__init__(name)
def next(self):
for line in file.__iter__(self):
r,c = ",".split(line[:-1]
yield self.affine(r,c)
Thanks in advance!
dave
<David Morgenthaler> wrote in message
news:ti********************************@4ax.com...How does one overide the iterator implied by the construct "for line
in file:"?
For example, suppose I have a file containing row,col pairs on each
line, and I wish to write a subclass of file that will transform these
to x,y coordinates using an affine transform. I''d like it to look
something like this (but this clearly doesn''t work):
class myFile(file):
def __init__(self,name,affine):
self.affine = affine
file.__init__(name)
def next(self):
for line in file.__iter__(self):
r,c = ",".split(line[:-1]
yield self.affine(r,c)
Thanks in advance!
I''m not sure what you''re trying to do, but you may be
looking for the readline() method.
John Roth
dave
David Morgenthaler wrote:
How does one overide the iterator implied by the construct "for line
in file:"?
''for'' uses __iter__().
Subclass file and redefine __iter__(self). It should return an object that
has the next() method which returns the next item, or raises StopIteration
if finished. Returning self from __iter__() is ok (then you can put the
next() in the same class).
An easier alternative is to make __iter__() a generator function (so calling
it automatically returns a ''generator'' object, which has next() on it, and
also raises StopIteration when done).
HTH,
Shalabh
"John Roth" <ne********@jhrothjr.com> wrote in message
news:10*************@news.supernews.com...
<David Morgenthaler> wrote in message
news:ti********************************@4ax.com...How does one overide the iterator implied by the construct "for line
in file:"?
Wrap it, don''t replace it. See below.
For example, suppose I have a file containing row,col pairs on each
line, and I wish to write a subclass of file that will transform these
to x,y coordinates using an affine transform. I''d like it to look
something like this (but this clearly doesn''t work):
Your class needs an __iter__ function. However, unless you have some other
reason for the heavy duty class option, much easier is something like:
def xycoords(lines):
# lines is an iterable that yields text lines of appropriate format
for line in lines:
<extract numbers and transform>
yield x,y
You can feed xycoords a literal list of lines for testing and then an open
file for production use.
Chaining iterators like this is part of their intended use.
Terry J. Reedy
这篇关于如何覆盖“for file in file:”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!