Namespace Qualification Question
Sean 'Shaleh' Perry
shalehperry at attbi.com
Fri Mar 22 14:47:26 EST 2002
On 22-Mar-2002 Craig McLean wrote:
> I've been messing around with python namespaces and I've run into
> something that I haven't been able to answer.
>
> Is there a module assosciated with the file you started the interpreter
> with, and if there is what is it's name? For instance
>
> > python qux.py
>
> I would have thought that there would be a qux module, and that it's
> name would be stored in the __name__ builtin.
>
> If I was using the interpreter interactively would that change things?
>
When run as a script its __name__ is '__main__'. This is how all of those
python modules allow themselves to contain test code.
def my_func(arg):
pass
if __name__ == '__main__':
my_func()
if you import the above code (say it is in my.py) in an interpreter session or
in a script the __name__ will be 'my'. If you run it as python my.py its name
is '__main__'.
More information about the Python-list
mailing list