Memory footprint of a class instance
sholden at holdenweb.com
Thu Feb 20 13:52:32 CET 2003
"Dennis Lee Bieber" <wlfraed at ix.netcom.com> wrote ...
> dromedary fed this fish to the penguins on Wednesday 19 February 2003
> 03:18 pm:
> > A beginner's question:
> > f.read()
> > I get the file contents. However, if I then once more use
> Not if you did the read on the open as you claim -- I get an error
> that a string object (f) doesn't have a read().
> > f.read()
> > I get " ". Apparently f.read() is only good for one use. To get around
I suspect that dromedary (a rather incendiary name for a Python user :-)
actually got "" rather than " ", but we'll let that pass.
> I'd expect EOF... the first f.read(), if done correctly, would
> read the entire contents of the file, so there is nothing left to read.
Indeed. It clearly doesn't make sense to call the read() method, which is
specifically intended to consume and return to the caller all the data in
the file, more than once on the same open file object. (Unless it's
something like a tty, which the OP's file clearly wasn't).
It's sometimes a surprise to newcomers that reading "past" the end of a file
doesn't raise any kind of exception, but this is clearly documented:
Read at most size bytes from the file (less if the read hits EOF before
obtaining size bytes). If the size argument is negative or omitted, read all
data until EOF is reached. The bytes are returned as a string object. An
empty string is returned when EOF is encountered immediately. (For certain
files, like ttys, it makes sense to continue reading after an EOF is hit.)
Note that this method may call the underlying C function fread() more than
once in an effort to acquire as close to size bytes as possible. """
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/
Register for PyCon now! http://www.python.org/pycon/reg.html
More information about the Python-list