How to overide "for line in file:"

Aahz aahz at pythoncraft.com
Sat Feb 21 21:25:47 EST 2004


In article <tisf301qf76c8lh0jkq9086gpqoep1bb1u at 4ax.com>,
 <David Morgenthaler> wrote:
>
>How does one overide the iterator implied by the construct "for line
>in file:"?

You don't.

>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. 

Don't.  Write a completely separate iterator (a generator) that does the
transform, which takes an iterator providing strings (lines) as its
input.  Then you can do this:

    for item in transform(f):

which should do what you want and be completely Pythonic.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python



More information about the Python-list mailing list