Help with Borg design Pattern

Casey McGinty casey.mcginty at gmail.com
Fri Jun 27 21:47:43 EDT 2008


On Fri, Jun 27, 2008 at 3:21 PM, Casey McGinty <casey.mcginty at gmail.com>
wrote:

> Hi,
>
> I'm trying to implement a simple Borg or Singleton pattern for a class that
> inherits from 'dict'. Can someone point out why this code does not work?
>
> class MyDict( dict ):
>    __state = {}
>    def __init__(self):
>       self.__dict__ = self.__state
>
> a = MyDict()
> a['one'] = 1
> a['two'] = 2
>
> print a
> print MyDict()
>
>
This looks like a good solution:

class MyDict( dict ):
   def __new__(cls,*p,**k):
      if not '_instance' in cls.__dict__:
         cls._instance = dict.__new__(cls)
      return cls._instance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080627/aa9a6434/attachment-0001.html>


More information about the Python-list mailing list