On Fri, Jun 27, 2008 at 3:21 PM, Casey McGinty <[EMAIL PROTECTED]>
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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to