Memory footprint of a class instance

dromedary camel at oasis.com
Wed Feb 19 18:18:39 EST 2003


A beginner's question:

Suppose I set up a class to handle files intended for text processing,
and I make a class that looks like so:

class FileInit:

   def __init__(self, path):
      self.pointer = open(path)
      self.string = open(path).read()  
      self.list = open(path).readlines()

Then I instantiate the class like so:

fh = FileInit('MY-PATH')

Have I then already read the file into fh.string and fh.list, or do
they exist only if I call them by, say, 

print fh.string


I'm using large lists of words, and I wonder if I'll gum up the works
by creating big list and string objects right off the bat.

BTW, the reason for the class is that when I've used this code

f = open('MY-PATH').read()

I get a perfectly nice pointer if I just put f on the command line and
hit return. If I then write

f.read()

I get the file contents. However, if I then once more use

f.read()

I get " ". Apparently f.read() is only good for one use. To get around
that I thoought I might use a class. If I'm mistaken in my assumptions
or there's a easier way to create strings and lists to slice and dice
from files, please let me know.


Jon




More information about the Python-list mailing list