PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed Need Money Easy and Quick?

Re: Python object overhead?

 List
Subject: Re: Python object overhead?
Poster: Gabriel Genellina
Date: Fri, 23 Mar 2007 19:21:09 -0300
Related Postings: 1 2 3 4 5 6 7
En Fri, 23 Mar 2007 18:11:35 -0300, Matt Garman 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

 

Page2Go2.com is not responsible for content of this message.