Singletons
Erik Max Francis
max at alcyone.com
Fri Apr 11 23:59:12 EDT 2003
Roy Smith wrote:
> It seems like a pretty reasonable implementation, but I don't
> understand
> one thing about it. What is the point of:
>
> # Store instance reference as the only member in the handle
> self.__dict__[ '_Singleton__instance' ] = Singleton.__instance
>
> I don't see that it does anything useful. Is there something subtle
> that I'm missing?
I presume you're confused about the '_Singleton__instance' key? It's
because a double underscore for a class or instance attribute is
mangled. For instance:
>>> class C:
... def __init__(self):
... self.__x = 123
...
>>> c = C()
>>> dir(c)
['_C__x', '__doc__', '__init__', '__module__']
>>> c.x
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: C instance has no attribute 'x'
>>> c._C__x
123
So this pattern is really just diddling with the new object and
assigning something to a "private" attribute (e.g., one prefixed with a
double underscore).
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ When you talk to her / Talk to her
\__/ India Arie
WebVal / http://www.alcyone.com/pyos/webval/
URL scanner, maintainer, and validator in Python.
More information about the Python-list
mailing list