[Tutor] Reading opened files

Lisi lisi.reisz at gmail.com
Fri Jun 17 18:58:12 CEST 2011


On Friday 17 June 2011 17:42:29 Walter Prins wrote:
> 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.

Thanks, Walter.  That is also very useful and clear.  As with James's answer, 
I have left this intact for the archives.  It should be available for other 
newbies who are blundering about a bit.

Lisi




More information about the Tutor mailing list