Memory footprint of a class instance

dromedary camel at
Thu Feb 20 00:18:39 CET 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

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

I get " ". Apparently 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.


More information about the Python-list mailing list