Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 22:48, 12 October 2007.

How to make a singleton in Python

From Forum Nokia Wiki

There are few more or less complicated ways to make singleton in Python.

One of them is to nest a class into a class and override the attributes getters and setters:

## Singleton class
#
class Foo( object ):
    ## Stores the unique Singleton instance-
    _iInstance = None
    
    ## Class used with this Python singleton design pattern
    #  @todo Add all variables, and methods needed for the Singleton class below
    class Singleton:
        def __init__(self):
            ## a foo class variable
            self.foo = None
 
    ## The constructor
    #  @param self The object pointer.
    def __init__( self ):
        # Check whether we already have an instance
        if Foo._iInstance is None:
            # Create and remember instanc
            Foo._iInstance = Foo.Singleton()
 
        # Store instance reference as the only member in the handle
        self.__dict__['_EventHandler_instance'] = Foo._iInstance
    
    
    ## Delegate access to implementation.
    #  @param self The object pointer.
    #  @param attr Attribute wanted.
    #  @return Attribute
    def __getattr__(self, aAttr):
        return getattr(self._iInstance, aAttr)
 
 
    ## Delegate access to implementation.
    #  @param self The object pointer.
    #  @param attr Attribute wanted.
    #  @param value Vaule to be set.
    #  @return Result of operation.
    def __setattr__(self, aAttr, aValue):
        return setattr(self._iInstance, aAttr, aValue)
 
 
## Test script to prove that it actually works        
if __name__ == "__main__":
 
    # create a first object
    a = Foo()
 
    # get and print class variable foo
    print a.foo
 
    # create a second object
    b = Foo()
 
    # set a string to the class variable foo
    b.foo = "Hello Folks"
 
    # create a third object
    c = Foo()
 
    # get and print class variable foo for object a
    print a.foo
 
    # get and print class variable foo for object c
    print c.foo

And you get:

None
Hello Folks
Hello Folks


But the method is not perfect because there will be incompatibilities and issues if you try to inherit that class because __getattr__ and __setattr__ are overridden.

Related Discussions
Thread Thread Starter Forum Replies Last Post
python with J2ME prashant332 Mobile Java General 1 2006-03-22 17:17
NoSteal KloNom Python 14 2008-08-12 18:14
How to minimize python app? fightersoft Python 9 2007-01-03 18:59
[Announce] RemoteKB 1.00.0 y.a.k Python 37 2008-09-07 07:28
Problem with sending SMS in Nokia 3200 from Java app destroyer2003 General Messaging 0 2004-09-22 12:12
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtopicQUqfnTopicZpythonQ
     qfnZtopicQUqfnTopicZseriesE5f60Q
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX