Re: Lazy lists for use with large dictionaries

scharf@EMBL-Heidelberg.DE
Wed, 28 Sep 1994 16:04:00 +0100

Guido van Rossum writes:
> I understand the principle, and am thinking of adding it to the
> language a a replacement of the internals of the for loop. It would
> make it possible e.g. to say "for line in file.lines(): ...", to loop
> over dictionaries etc.

Well, you can have this today!

#############################################################################
class File:
def __init__(self,name,mode='r'):
self.file=open(name,mode)
def __getitem__(self,i):
line=self.file.readline()
if not line:
raise IndexError
return line

file=File('File.py')

for line in file:
print line,
#############################################################################

Ok, file[i] has a very strange semantic.... :
file[i] is not file[i] :-)

Michael

P.S.: with the __{get,set,del}attr__ extension add:

class File:
...
def __getattr__(self,name):
return getattr(self.file,name)

and it will behave like a normal file....

-- 
         __________________________________________________________
   ****    ___   _  _   ___   _    
  ******  | __) | \/ | |   ) | |   Michael Scharf
 ******** | _)  |    | | -<  | |_  Tel: +49 6221 387 305 Fax: 517
  * ****  |___) |_||_| |___) |___) EMail: scharf@EMBL-Heidelberg.de
   ****   __________________________________________________________