[Tutor] Reading opened files

Walter Prins wprins at gmail.com
Fri Jun 17 18:42:29 CEST 2011


On 17 June 2011 17:20, Lisi <lisi.reisz at gmail.com> wrote:

> >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r")
> >>> file.close()
> >>> file=open("/home/lisi/CHOOSING_SHOES.txt", "r")
> >>> whole=file.read
> >>> print whole
> <built-in method read of file object at 0xb74c48d8>
> >>> print "%r" % whole
> <built-in method read of file object at 0xb74c48d8>
> >>> print "whole is %r" %whole
> whole is <built-in method read of file object at 0xb74c48d8>
> >>> print "whole is %r" % whole
> whole is <built-in method read of file object at 0xb74c48d8>
> >>>
>

You're missing the () off the whole=file.read() call.

Ask youself, what is "file.read"?   It is of course a method of the "file"
object.  And, in fact that's exactly what Python itself is telling you also.
  So when you say:

whole=file.read

You're assigning the method itself, to the name "whole".  Consequently, you
would be able to do:

something = whole()

... which would then *call* the function using the name "whole", which would
be identical to calling that same function via "file.read".

To reiterate, there's a difference between just referencing a method or
function and actually calling it.  To call it you need to use parentheses.

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110617/b15ce89d/attachment.html>


More information about the Tutor mailing list