Memory footprint of a class instance

Rene Pijlman at de.nieuwsgroep
Thu Feb 20 02:20:19 CET 2003

>   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

Yes, __init__ is executed when a class instance is created.

>or do they exist only if I call them by, say, 
>print fh.string

No. But you could implement it that way of course, by moving
read() out of __init__ and into string (which would need to be a
method or a property, not a normal attribute).

See aso
for another trick. A bit complicated if you ask me, but some
would perhaps call it Pythonic :-)

>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. 

The above code reads the contents of the file into a string f.

This is what I see:

>>> f = open("d:\\x.txt").read()
>>> print type(f)
<type 'str'>
>>> print f

And so I don't understand the rest of your story about the
behavior of read().

>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.

The problem may be in my limited grasp of the English language,
but I don't quite understand what you mean here.

René Pijlman

Wat wil jij leren?

More information about the Python-list mailing list