[Tutor] basic import question

Jeff Shannon jeff@ccvcorp.com
Tue Jul 8 14:53:01 2003


Chris Kassopulo wrote:

>def main():
>    print "hello world"
>main() 
>
>If "import mymain" runs mymain completely from the
>top down, why doesn't this run twice, once for the
>import and once for the call ?
>

Why does "hello world" only print once?  Because you're only telling it 
to print once...  Defining a function does not run the code that the 
function contains, only calling a function does.  

There's two code blocks in this file.  (Any line followed by a series of 
further-indented lines can be seen as a code block.)  Everything in the 
block that starts "def main():" is processed to create a function 
object, which is bound to the name "main", but it does *not* get 
executed at the time that it's defined.  The second code block consists 
of the single statement "main()", which then executes the function 
object that was previously created.

Hope that makes a bit more sense now...

Jeff Shannon
Technician/Programmer
Credit International