Python object overhead?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Mar 23 18:21:09 EDT 2007
En Fri, 23 Mar 2007 18:11:35 -0300, Matt Garman <matthew.garman at gmail.com>
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
More information about the Python-list
mailing list