Memory footprint of a class instance

Anna revanna at mn.rr.com
Wed Feb 19 23:54:11 EST 2003


On Thu, 20 Feb 2003 02:20:19 +0100, Rene Pijlman wrote:

> dromedary:
>>   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
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/131495 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
> bla
> 
> 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.


Okay, back to the problem with assignments by the OP.

FWIW - I couldn't get it to accept .read() at BOTH the assignment
statement *AND* the call statement...

>>> f=open("//home//anna//Documents//neruda.txt").read() 
>>> f.read()
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in ?
    f.read()
AttributeError: 'str' object has no attribute 'read'
>>> 
>>> 

So, I tried the following, so at least I could replicate the problem OP
was describing. 

>>> f=open("//home//anna//Documents//neruda.txt") 
>>> f.read()
"LXVI\n\nNO TE QUIERO sino porque te quiero\n"
>>> f.read()
''
>>> print f
<open file '//home//anna//Documents//neruda.txt', mode 'r' at 0x835ffb0>
>>> print type(f)
<type 'file'>

What I don't get is why you would do f.read() again...(Actually, I'm not
completely sure why you did it in the first place, but I'll get into that
below.) Anyways, it looks like calling f.read() again reassigns f to the
open file object(?) instead of to the string that resulted from opening
and reading the file in the first place.(I'm sure one of the experienced
folk here can explain what's really happening better than I can. I'm sure
my explanation is clear as mud...)

OTOH - if I do it this way:

>>> f=open("//home//anna//Documents//neruda.txt").read()

and then try:
>>> f
"LXVI\n\nNO TE QUIERO sino porque te quiero\n"
>>> f
"LXVI\n\nNO TE QUIERO sino porque te quiero\n
>>> print f
LXVI

NO TE QUIERO sino porque te quiero
>>> print type(f)
<type 'str'>
>>> print f[:40]
LXVI

NO TE QUIERO sino porque te quiero
>>>
>>>
All of which looks like it works just fine to me...

Anyways -

>>> f=open("//home//anna//Documents//neruda.txt").read()

assigns the *results* of opening and reading the file (i.e. a string) to
f. And you can do whatever you want to the string called f. You can slice it and
index it and make julienne fries. So, it seems to me, the easiest solution
would be to assign f to the string and then stop reassigning it to other stuph.

<Like the old joke: Man goes in to the doctor and sez: "Doc, it hurts when
I move my arm like this. What should I do?" Doc answers "Stop moving your
arm like that.">

But, if you really want f to mean "apply open to the file object..." which
you then call with argument read() (or whatever other argument it takes, I
suppose), I played with it some more and came up with the following:

>>> f=open("//home//anna//Documents//neruda.txt") 
>>> poem=f.read()
>>> poem
"LXVI\n\nNO TE QUIERO sino porque te quiero\n
>>> print poem
LXVI

NO TE QUIERO sino porque te quiero
>>> f
<open file '//home//anna//Documents//neruda.txt', mode 'r' at 0x83a0d50>
>>> poemtitle=poem[:4]
>>> print poemtitle
LXVI
>>> 

This way: f isn't the string, f is the open file object. 

And the string has been assigned to its very own name: poem. So you can
have your string and slice it too.

Just my $.03 worth.
Anna
--
RoncoPy: But Wait! There's MORE...!




More information about the Python-list mailing list