Borg Pattern Class usable from other classes?

Jan Wender ian at atlas.science-computing.de
Fri Oct 12 10:19:38 EDT 2001


Duncan Booth <duncan at nospamrcp.co.uk> wrote:
[...]
> 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.

This makes it perfectly clear to me, thanks a lot!

> 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.
That I found yesterday also, but without an idea why it helped.

Erik Max Francis wrote in <3BC5E0A7.41050647 at alcyone.com>:

>The sequence appears strange anyway.  a.py defines the support classes
>and b.py defines the test harness, but the __main__ clause of a.py
>invokes the test harness?  Why not put the test harness where it
>belongs, in b.py with the Test class?  That will eliminate your
>recursive import problem.

Well, this was not about a test harness. The class got named Test
while I was trying to find a minimal case which shows the problem. I
use the Borg class to contain configuration information like username,
password etc.

And only lead your singleton if it is not in the trump suit, because
that would be pointless.
Cheerio,
-- 
J.Wender at science-computing.de  -  Fon +4970719457-257 Fax-211
science+computing ag - Hagellocher Weg 71-75 - 72070 Tübingen
LISP is worth learning for the profound enlightenment experience you will have
when you finally get it; that experience will make you a better programmer for
the rest of your days, even if you never actually use LISP itself a lot. (esr:
How To Become A Hacker)



More information about the Python-list mailing list