Thanks all. <div>I think my fundamental error was in thinking of <i>menu.tx</i>t as the file and <i>inp</i> as containing it's contents in some way. Much more helpful to think of <i>inp</i> as the file and then do things to it.<div>
<br></div><div>Thanks again</div><div><br></div><div>Neil</div><div><br clear="all">This email is confidential and intended for addressee only .<br>
<br><br><div class="gmail_quote">On Mon, Jun 21, 2010 at 1:37 AM, Alex Hall <span dir="ltr"><<a href="mailto:mehgcap@gmail.com">mehgcap@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 6/20/10, Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br>
> On Mon, 21 Jun 2010 09:02:55 am Alex Hall wrote:<br>
>> On 6/20/10, Neil Thorman <<a href="mailto:neil.thorman@gmail.com">neil.thorman@gmail.com</a>> wrote:<br>
> [...]<br>
>> >>>>inp = file("menu.txt", "r")<br>
>> ><br>
>> > *What is inp? What does it now contain?*<br>
>><br>
>> It is now a reference to the location of the txt file.<br>
><br>
> [pedantic]<br>
> No, it's actually an abstract data structure that wraps the actual file<br>
> itself. A reference to the *location* of the file would be:<br>
><br>
> inp = "menu.txt"<br>
><br>
> Locations are strings. File objects are file objects.<br>
> [/pedantic]<br>
><br>
> But in a practical sense, pay no attention to the man behind the curtain<br>
> (the implementation details). inp is a file in every way which matters,<br>
> just like after:<br>
><br>
> n = 42<br>
> x = 1.234<br>
><br>
> n is an int and x a float in every way which matters.<br>
><br>
><br>
>> Python calls<br>
>> these file "objects", where an object is just something on which you<br>
>> can call functions. If you had a dog object you might call a "bark"<br>
>> method; here, we have a file object, so we can see what the file is.<br>
>> inp is not the file itself, just as any object is not the full<br>
>> object's info but rather a pointer to where that info is. If you were<br>
>> to print inp, I think you would get a memory address.<br>
><br>
> You would get a string printed to the console, like printing *anything*.<br>
><br>
>>>> print inp<br>
> <open file 'foo', mode 'w' at 0xb7d3a250><br>
</div></div>Oh right, the object's toString method (or whatever Python calls this;<br>
Java and Javascript call it toString, and it exists for all objects).<br>
<div class="im">><br>
> That string happens to contain a hex number which looks like it could be<br>
> a memory address, but that's an implementation detail because CPython<br>
> doesn't sufficiently abstract its objects from the underlying C<br>
> implementation.<br>
><br>
> Python file objects aren't "files" only in the sense that they exist in<br>
> memory rather than on disk in the file system, but other than that, I<br>
> believe your explanation is at too low a level to be helpful. Neil said<br>
> he's a beginner who hasn't done any programming since BASIC on an<br>
> Acorn, and you're talking about low-level details like memory<br>
> locations. Let me guess, you're also a C programmer?<br>
</div>Good point, and sorry for going into too much detail, much of which is<br>
not on the mark anyway. :) No, I have hardly touched c++, but I am<br>
starting my final year of a computer science degree in a few months so<br>
I have had all the details of objects and how the computer actually<br>
accesses them in several classes.<br>
<div class="im">><br>
> As far as coding in Python is concerned, inp = file(pathname) creates a<br>
> file object, which *is* a file in all practical sense. Everything else<br>
> is just an implementation detail, which could change depending on the<br>
> version of Python, the operating system, and the underlying hardware.<br>
</div>Very true.<br>
<div class="im">><br>
><br>
> [...]<br>
>> >>>>print inp.readlines()<br>
>> ><br>
>> > ['spam & eggs\n', 'spam & chips\n', 'spam & spam']<br>
>> ><br>
>> > but if I do it again I get:<br>
>> >>>> print inp.readlines()<br>
>> ><br>
>> > []<br>
>> ><br>
>> > I'm baffled, why is inp now empty?<br>
>><br>
>> I suspect you have hit the end of the file.<br>
><br>
> Yes. readlines reads from the current file position, like all read<br>
> operations. To read all the text again, you have to reset the file<br>
> position with seek:<br>
><br>
> inp.seek(0)<br>
><br>
><br>
><br>
> --<br>
> Steven D'Aprano<br>
> _______________________________________________<br>
> Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
> To unsubscribe or change subscription options:<br>
> <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
><br>
<br>
<br>
</div><div class="im">--<br>
Have a great day,<br>
Alex (msg sent from GMail website)<br>
<a href="mailto:mehgcap@gmail.com">mehgcap@gmail.com</a>; <a href="http://www.facebook.com/mehgcap" target="_blank">http://www.facebook.com/mehgcap</a><br>
</div><div><div></div><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div></div>