Why don't a singleton-shaped Thread-subclass instance's attributes of type Event return the same value?
Question: I have this part of code:

    
    
    from threading import Thread, Event
    
    class mySubThread(Thread):
    
        instance = []
    
        def __new__(cls):
            if not cls.instance:
                cls.instance.append(object.__new__(cls))
            return cls.instance[0]
    
        def __init__(self):
            self.enabled = Event()
            self.exit = Event()
            Thread.__init__(self)
    
        def run(self):
            while self.enabled.wait():
                if self.exit.is_set():
                    break
                # ... boring useful stuff ...
    
        enable = lambda self: self.enabled.set()
        disable = lambda self: self.enabled.clear()
    
        def quit(self):
            self.enabled.set()
            self.exit.set()
    

When I set attributes which are instances of class `Event`, I get different
values from their method `.is_set()` whether I take the attribute from
`mySubThread()` or a name which is initialised as equal to it. And if I get
the method from `mySubThread()`, then, from the named one, I see the value
obtained is then equal to `mySubThread.attr.is_set()`. To make it more clear
(even to me, what I wrote seems a bit confused), I have:

    
    
    >>> named = mySubThread()
    >>> named.enabled.is_set()
    False
    >>> named.enable()
    >>> named.enabled.is_set()
    True
    >>> named is mySubThread()
    True
    >>> mySubThread().enabled.is_set()
    False
    >>> named.enabled.is_set()
    False
    

While `mySubThread().enable()` sets `x.enabled.is_set()` to `True`,
`mySubThread().enabled.is_set()` is always `False`.

Does anyone know if I did anything wrong, or is it a bug in Python?

I work on OS X.9.1 with Python 3.3.3 and GCC 4.2.1.


Answer: `__init__` is called on the object returned from `__new__`. When you call
`mySubThread()` the second time, your `__new__` returns a reference to the
`named` object, and your `__init__` overwrites its attributes.

