static class variables and module scope

The Dark Seraph chris at acheris.net
Mon Jul 19 10:37:24 EDT 2004


I have a singleton object that works perfectly from inside my main .py
file.  However, when I want to access the object that backs the
singleton from another file ( either from an execfile() or thru my
telnet listener ), it appears the underlying object is reset to null,
resulting in a duplicate object being created.  

I figure I am doing something painfully stupid, but for the life of me,
I can't see it.  

Here is my singleton wrapper:

class SingletonWrapper:
        __instance = None
        def __init__ ( self, user = None, password = None ):
                if SingletonWrapper.__instance is None:
                        SingletonWrapper.__instance = ImplSingletonWrapper ( user, password )
        def instance ( self ):
                return SingletonWrapper.__instance

In my ImplSingletonWrapper class, I print out that I am creating a new
object when its constructor is called.  Inside the owner file, it is
only called once , but if I do SingletonWrapper().instance() from
anohter module, it makes a new object, and id() reports different ids.

I banged my head against this yesterday, and today doesn't seem to be
going any better, any ideas out there?

-- 
chris at acheris.net			|  Fight Censorship, Boycott Wal-Mart
		Now is the winter of your discontent! - Stewie



More information about the Python-list mailing list