[Tutor] Question about dictionary method get()

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Jun 29 04:10:07 EDT 2004



> Thanks very much again, Francis. Very clear, except for my not understanding
>
> if __name__ == '__main__':
>          func()
>
> But that's OK. I'll get to that later.

Hi Dick,


Ah.  Unwritten community knowledge.  *grin* Take a look at:

    http://diveintopython.org/getting_to_know_python/testing_modules.html

which explains that construct in detail.  Here's a three-sentence
overview:

When a Python program file is executed directly, a special variable called
'__name__' is set to the string "__main__".

The same program file, however, can be used as a module too, and on an
'import', the '__name__' ends up being something else.

So the 'if' statement above lets us do something different if the program
is being run directly, as opposed to being imported.


Hope this helps!




More information about the Tutor mailing list