namespace confusion
Harald Kirsch
kirschh at lionbioscience.com
Thu Aug 9 03:33:09 EDT 2001
Mark Robinson <m.1.robinson at herts.ac.uk> writes:
> I have a program that is organised in three files as follows:
>
> #main.py
> #########
>
> from first import *
> from second import *
>
> functfirst() #from first
> functSecond() #from second
>
> ########################
>
> #first.py
> ##########
>
> functfirst()
>
> ************
>
> #second.py
> ##########
>
> from first import *
>
> def functCecond():
> functfirst() #
>
> -----------------------------------------------
>
> The problem I am having is I can't access the functfirst() from
> second.py.
Its easier than you think. You don't import first.py into second.py
and so you don't access to its functions. The reason is that every
import creates a totally new namespace for the created module. AFAIK
that namespace contains by default always the same things for each and
every module. You can change that default population only with
`import' statements in the imported module.
It is *not* the case that an imported module inherits its globals()
from the importing module. Try this:
## main.py
GAGA = 1
import first.py
## first.py
print GAGA
and you'll get an error message precisely because the GAGA being
global in main.py is *not* inherited in the globals of the imported
first.py .
Insofar two consecutive import statements as you use behave quite
different from two consecutive #include directives in a C program.
Harald Kirsch
--
----------------+------------------------------------------------------
Harald Kirsch | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172 | -- Paul Erdös
*** Please do not send me copies of your posts. ***
More information about the Python-list
mailing list