Borg Pattern Class usable from other classes?

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Oct 12 10:26:21 CEST 2001


Jan Wender <j.wender at science-computing.de> wrote in 
news:mailman.1002804792.23588.python-list at python.org:

> The two files:
> # a.py
> class Borg:
...
> if __name__ == "__main__":
...
>     import b
> # b.py
> import a
...
> 
> Thanks for any insights,
> 
If you run a.py as a script then it executes as the module '__main__'. This 
is why you can do the test for the current module name being '__main__'. If 
you import a from another module then it runs as the module 'a'. Even 
though both modules '__main__' and 'a' are executing from the same file 
they have completely separate namespaces.

So when b imports a it is going to see a brand new Borg class, not the one 
you created in your __main__ script.

The solution is to put your Borg class in a separate module, or more 
generally never try to import anything from the __main__ script that you 
use to kick off the program.


-- 
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