What is the base namespace?

This is the place for queries that don't fit in any of the other categories.

What is the base namespace?

Postby pnelsonsr » Tue May 06, 2014 3:49 pm

I've been coding in python and using classes so I have things pretty sewn up in namespaces. But I was wondering what is the base/root namespace's name?
pnelsonsr
 
Posts: 29
Joined: Thu Dec 12, 2013 12:48 am

Re: What is the base namespace?

Postby micseydel » Tue May 06, 2014 6:46 pm

Code: Select all
print __name__

I believe the answer to your question is
Code: Select all
"__main__"
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1443
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: What is the base namespace?

Postby pnelsonsr » Tue May 06, 2014 9:20 pm

Hmm...

I'm trying to figure out how I would test this assumption!
pnelsonsr
 
Posts: 29
Joined: Thu Dec 12, 2013 12:48 am

Re: What is the base namespace?

Postby 7stud » Tue May 06, 2014 9:52 pm

I've been coding in python and using classes so I have things pretty sewn up in namespaces.

Do you know about modules and packages? Classes aren't the only things that create namespaces in python.

pnelsonsr wrote:Hmm...

I'm trying to figure out how I would test this assumption!

Hmmm...do you recognize a distinction between scope and namespace? I guess a base namespace would be the top package in a package/module hierarchy that you created. On the other hand, if you don't have a package hierarchy then you should be aware that every python file is a module; and every variable, function, and class is namespaced by the module name.

As far as scope goes, it's the famous LEGB. And for __main__, the docs say:

__main__ — Top-level script environment
This module represents the (otherwise anonymous) scope in which the interpreter’s main program executes


...which seems to indicate a scope hierarchy of LEGB__main__, which, in my opinion, is just gobbledygook, with the only practical application I know of being:

Code: Select all
if __name__ = "__main__":
    #The following code will run if this file was specified as the argument to `python`.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am

Re: What is the base namespace?

Postby pnelsonsr » Fri May 09, 2014 5:20 pm

Thanks for the response.

Yes I know about and use modules and that might be where I my question might stem from. When I use classes and modules they all have a very distinct namespace and scope. But the root or __main__ seems different and I have no access to it. I've come up with cool ways around this but sometimes I feel like I'm missing something.
pnelsonsr
 
Posts: 29
Joined: Thu Dec 12, 2013 12:48 am

Re: What is the base namespace?

Postby 7stud » Fri May 09, 2014 7:11 pm

Maybe you could provide a concrete example that does not allow you to do what you want and requires a cool work around.
7stud
 
Posts: 106
Joined: Wed Apr 02, 2014 2:36 am


Return to General Coding Help

Who is online

Users browsing this forum: l_mono and 4 guests