singleton objects with decorators
Bengt Richter
bokr at oz.net
Tue Apr 12 09:26:52 EDT 2005
On Tue, 12 Apr 2005 13:26:54 +0200, "Fredrik Lundh" <fredrik at pythonware.com> wrote:
>Bengt Richter wrote:
>
>> But isn't bool supposed to be a singleton class/type ?
>>
>> >>> [bool(x) for x in 0, 0.0, [], {}, False]
>> [False, False, False, False, False]
>> >>> [id(bool(x)) for x in 0, 0.0, [], {}, False]
>> [505014288, 505014288, 505014288, 505014288, 505014288]
>
>"False" is an ordinary global object, not an object factory or a singleton class.
>
UIAM, "False" is a string, or a representation of a string ;-)
And the name False is typically found as __builtins__.False unless you
have shadowed it locally or in some module globals, as I'm sure you know.
I was just demonstrating that all the bool(x) _instances_ are the identical
False object in the above, and duck-wise that quacks singleton-like,
even though the True maybe makes it a dualton, since bool does return two distinct instances ;-)
IMO the global False name accessibility is a red herring, since it's the object it is
normally bound to that is the real subject.
>>> __builtins__.False is bool(0)
True
UIAM, the True and False objects per se are unique and built into boolobject.c
And you can bind all the names you like to them, but bool(0) is always that single
unique object and so is bool(1) its unique object. I don't believe this is an
optimization like that involving the first 100 integer values or so.
It's a weird beast, being a subtype of int also. I'll defer to the BDFL in
http://www.python.org/peps/pep-0285.html
"""
The values False and True will be singletons, like None. Because
the type has two values, perhaps these should be called
"doubletons"? The real implementation will not allow other
instances of bool to be created.
"""
Regards,
Bengt Richter
More information about the Python-list
mailing list