source: SHX/trunk/sandbox/singleton.py @ 116

Revision 116, 745 bytes checked in by marcus, 11 years ago (diff)
  • refactoring for use as later python module
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Rev Id Date
Line 
1# -*- coding: utf-8 -*-
2
3""" from singleton to multiton """
4
5import time
6
7class sngl(object):
8    _instances = {}
9#    sh = None
10   
11    def __new__(type, *args, **kwargs):
12        try:
13            inst = args[0]
14        except IndexError:
15            raise TypeError, "class needs one unique identifier"
16
17        """ ensure single instance of identifier """
18#        if not '_the_instance' in type.__dict__:
19        if not args[0] in type._instances:
20            type._instances[args[0]] = object.__new__(type)
21           
22        return type._instances[args[0]]
23
24    def __init__(self, *args, **kwargs):
25        if not "sh" in self.__dict__:
26            # load shared lib (once)
27            self.sh = time.time()
28           
29a = sngl("aaa")
Note: See TracBrowser for help on using the repository browser.