namespace query

D

Dr Mephesto

Hi,

I have a quick question about global namespace, and I'm hoping someone
could give a quick reply to sort me out :)

I have a single file program that uses numpy, and it works just fine.
I want to move some classes into their own files, to make the code
reusable. When I cut and paste the the classes into new files, and
then "import" them back into the original program, I get an
error,:"NameError: name 'numpy' is not defined", called about the new
class files.

If I add a "global numpy" to the beginning of each class in the new
files, the program runs. Do I really have to add "global XXX" for
every module I import in the main program into every module I create
and import? Why are the class files I created not seeing the top
namespace?

Thanks in Advance!

Dave
 
M

Marco Mariani

Dr said:
Why are the class files I created not seeing the top namespace?

Because it's how python works. What you think is a top namespace, it's
not "at the top". It's just the namespace of the module you run the
program with. You must import numpy from the all the modules that make
use of it.

I doubt your "global" fixed something, I'd like to see an example.
 
S

Steven D'Aprano

If I add a "global numpy" to the beginning of each class in the new
files, the program runs. Do I really have to add "global XXX" for every
module I import in the main program into every module I create and
import? Why are the class files I created not seeing the top namespace?

What's the top namespace?

Every Python module is a namespace. The module namespace is flat: all
modules are at the same level. A module can't know what other modules
have imported it, and code in the imported module can't see the importing
namespace.

However, you can get something like a hierarchical namespace by using
packages. Possibly this might accomplish what you are trying to do.

http://docs.python.org/tutorial/modules.html#packages
 
P

Peter Otten

Dr said:
I have a quick question about global namespace, and I'm hoping someone
could give a quick reply to sort me out :)

I have a single file program that uses numpy, and it works just fine.
I want to move some classes into their own files, to make the code
reusable. When I cut and paste the the classes into new files, and
then "import" them back into the original program, I get an
error,:"NameError: name 'numpy' is not defined", called about the new
class files.

If I add a "global numpy" to the beginning of each class in the new
files, the program runs. Do I really have to add "global XXX" for
every module I import in the main program into every module I create
and import? Why are the class files I created not seeing the top
namespace?

The global namespace in Python is actually a module-wide namespace. You have
to put an

import numpy

at the top of every module that uses numpy.

I lack the fantasy to imagine what exactly you have done that "global numpy"
has the described effect. It's probably not pretty.

Peter
 
D

Dr Mephesto

ok, sorted. I had thought that when a module was imported, it was
added to a larger shared namespace used by all the modules.


And yes, you are all correct; the "global numpy" thing was an illusion
caused by saving the file at the wrong time after making a few
changes.
 
M

Marco Mariani

Dr Mephesto wrote:

ok, sorted. I had thought that when a module was imported, it was
added to a larger shared namespace used by all the modules.

Now, that would be awfulll

Because one of the most important things about python (and the reason I
can live without an IDE) is that I can point my finger to an object
somewhere and tell you where it comes from, without loading a different
source file.

For the same reason, import * is frowned upon, especially because it's
transitive (and contagious).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top