En Fri, 23 Mar 2007 18:11:35 -0300, Matt Garman <[EMAIL PROTECTED]> escribió:
> Example 2: read lines into objects: > # begin readobjects.py > import sys, time > class FileRecord: > def __init__(self, line): > self.line = line > records = list() > file = open(sys.argv[1]) > while True: > line = file.readline() > if len(line) == 0: break # EOF > rec = FileRecord(line) > records.append(rec) > file.close() > print "data read; sleeping 20 seconds..." > time.sleep(20) # gives time to check top > # end readobjects.py Your file record requires at least two objects in addition to the line itself: the FileRecord instance and the dictionary instance (__dict__) used to hold its attributes. You can use a new style class with __slots__ to omit that dictionary: class FileRecord(object): __slots__ = ['line'] def __init__(self, line): self.line = line or defer instance creation: class FileRecords(list): # store here all the lines, maybe using readlines() def __getitem__(self, index): return FileRecord(list.__getitem__(self, index)) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list