namespace confusion

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Aug 8 15:23:26 CEST 2001


Mark Robinson <m.1.robinson at herts.ac.uk> wrote in 
news:3B7135E2.40701 at herts.ac.uk:

> The problem I am having is I can't access the functfirst() from 
> second.py. It makes no differce if I import it or not. What am I doing 
> wrong or not doing that I should?

The example you posted has several mistakes, so it isn't immediately clear 
which are mistyping and which might be your actual problem.

The spelling of functSecond/functCecond seems to vary, but this probably 
isn't related to your query.

Your first.py contains a call to functfirst, but no actual definition. 
Perhaps you cannot call the function because you haven't defined it?

Alternatively perhaps you have a different first.py that you failed to post 
which really does define the function but has some other lines in it as 
well. If it happens to try to 'import second' then this would also cause 
the problem you have described, because the 'from first import *' in 
second.py will be executed before first.py has created any variables to be 
imported.

But I think the real problem is that you are using 'from module import *'. 
This form of the import statement makes a copy of all the variables that 
exist in another module at the time when the import is executed. This is 
very rarely what you want. It is almost always better to just import the 
module and access the values when you need them. So second.py becomes:

import first

def functSecond():
	first.functfirst() #

and provided you actually define the function in first this call should 
work.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list