[Tutor] Help

Dave Angel d at davea.name
Tue Nov 1 13:35:33 CET 2011

(Pleas put your reply after the part you're quoting.  What you did is 
called top-posting, and makes reading the messages very confusing)
On 11/01/2011 12:10 AM, Chris Kavanagh wrote:
> I'm going to thank Steven once again, and answer my own question in 
> the 2nd paragraph directly below (Steven hasn't had a chance to 
> respond yet).
> I just learned that the Function definitions are not read by the 
> interpreter UNTIL they are called. I was reading them and assuming 
> they were executed from the top down. Obviously I was getting somewhat 
> confused because of this. This 'dragon' program makes much more sense 
> to me now that I understand that. . .Thanks again Steven & everyone!!!
> Happy Halloween!!!
That's not correct either.  The  interpreter definitely reads the file 
from top down.  But sometimes the effect of the code in this phase is to 
build some structures for later use.  For example the def keyword says 
that the line and some following ones are to be compiled into an object 
form representing a function.  Default arguments are exececuted at this 
time, but none of the rest.  it's compiled into a function object, and 
saved in the current namespace (typically the global one) for later 
use.  Then when the interpreter encounters a function call, it looks up 
the name, and if it's "callable"  (as a function object is), it gets its 
parameters filled in and gets used.

Then when that function returns, the interpreter continues interpreting 
till the end of file.

But notice that when you are executing inside a function, you may 
encounter references to the same or other functions.  At this point they 
do need to have been compiled into callable objects.  So while the order 
of the function definitions doesn't matter for calls made from the end 
of the file, you can get into trouble if some function is called which 
tries to call another one not yet compiled.  To avoid this possibility, 
the standard paradigm is to put all top-level code at the end of file 
(and inside an  if __name__ == "__main__"    clause) .

> On 10/31/2011 11:07 PM, Chris Kavanagh wrote:
>> Yes Steven, that solved my question(s). It also cleared up what was to
>> be my next question! Thanks so much. You might not realize how grateful
>> I am to be able to have you & others on the list answer my questions.
>> Just trust me when I say, I am grateful. And I apologize for the code
>> being mangled on your end. I'm not sure why this happened, but you were
>> correct in your assumptions. . .
>> One thing I'm curious about. If the code is read by the Interpreter or
>> Compiler from the top down, why in this case, does it not get confused
>> (cause an error) when it hits line 30, if it doesn't yet know that
>> {caveNumber=chooseCave()} and checkCave(caveNumber)}?? It seems to me in
>> my limited experience, those last two lines should've come before line
>> 30. It seems as though it would've made more sense to somehow put them
>> before. In other languages, ala C++, don't global variables have to be
>> declared at the 'top' of the code??
The main reason that C and C++ require declarations before use is that 
different kinds of variables take differing spaces, and the compiler 
figures out the memory layout before generating the code.  The compiler 
would have to be much more complex if it didn't have those 
restrictions.  In python, symbols ( colloquailly called "variables") 
don' t have any type, and can refer to different kinds of objects at 
different times.



More information about the Tutor mailing list