Yeah, and the particular implementation described there doesn’t allow ANY construction of instances of that class - instead, you have a getInstance()
static method. There are multiple ways to achieve it, but the constant is that there’s only the one instance.
Looks like Lucas isn’t going to answer, so here’s my point anyway: Assuming the meaning of “1
is a singleton” to be that there can’t be another int
object with value 1, so that x is 1
is “safe” (equivalent to x == 1
) if you only have int
s, that might not be true. In CPython 3.10, we could still build such an object, for example with (99**99 + 1) % 99**99
. That doesn’t work anymore in 3.11, but I don’t know whether there aren’t any other ways left, and I would’ve asked how they know there aren’t any.
Does the Gang of Four’s singleton pattern have the monopoly on the word “singleton”?
The docs also call True
, False
and enum members “singletons”.
Ofcourse there are other uses of the word singleton. But can you give an alternative definition that doesn’t become useless very quickly? (the one given in the docs does become useless). Wikipedia lists a few, but none except the singleton pattern really apply.
Well, this is not true. tuple()
will return you the ()
singleton, but tuple
is not a singleton type.
Like the one I mentioned for 1
:
Call an object a singleton if it’s the only possible object of the same type with the same value, i.e., you can’t have another object of the same type and they’re equal (as judged by ==
).
At least that’s the reasoning I imagine behind calling True
, False
, enum members, ()
and object()
singletons.
(And disregard trickery like creating duplicate objects by using ctypes
or C extensions or even modifying CPython, that’s not meaningful.)
Okay, I can agree, but why can’t I call 1
a singleton?
There are objects that the python language specification will define as singletons and you can rely on is
working reliably for them.
There are objects that a python implementation may optimise to seem to be a singleton, but is
cannot be relied to always work for them.
I’d guess that ()
is an implementation detail.
What’s your definition of “singleton”?
This definition means that all instances of custom classes that don’t override __eq__
are singletons. That doesn’t sound like a useful definition.
Excuse me, but now I’m much more confused.
True / False are not singletons? Why?
And obect()
, as far as I know, returns a singleton that can be used as a sentinel. Why can’t I name it singleton?
Hmm, but the docs call it a singleton, and not in a “CPython implementation detail” box. In my understanding, that makes it a guarantee.
The only thing I saw that really says it’s not a singleton is the "the constructor creates a new empty tuple”, but as tested, that’s not true.
The cases where it says “an empty tuple” instead of “the empty tuple” are compatible with it being a singleton (i.e., if it’s indeed a singleton, calling it “an” is misleading but not wrong).
()
is a singleton object of the type tuple[()]
, but not of the class tuple
. I am making a distinction there.
And either way, this is an implementation detail and not guaranteed by the language specification.
When people talk about the “singleton pattern”, value is not usually relevant. The concept of comparing it to another object simply doesn’t exist. So generally it means “the only possible object of that type”. For situations where there’s only ever one object of a type and value, that’s usually called interning.
I don’t think Python ever guarantees that there’s only one empty tuple object, so I’d call that one merely interned, too. Definitely integers.
As we already established, the docs usage of the term “singleton” is unreliable. The Python Docs are not a specification, they are an incomplete reference manual that mostly describes CPython.
Earlier I searched singleton definition
and Google showed “a single person or thing of the kind under consideration”. And I’d for example say True
is the only true bool
instance, i.e., the only one of its kind, where kind = “true bool
instance”. Or a red swan might be the only one of its kind, making that a singleton in that sense.
Did we? Don’t they all fit the definition I gave? Or all fit the definition “Comparing them with is
is the right thing to do”?
Yes, it’s because __eq__
can be overloaded.
>>> from unittest.mock import ANY
>>> ANY == None
True
>>> ANY is None
False
In 3. Data model — Python 3.12.1 documentation I see this text:
Tuples
The items of a tuple are arbitrary Python objects. Tuples of two or more items are formed by comma-separated lists of expressions. A tuple of one item (a ‘singleton’) can be formed by affixing a comma to an expression (an expression by itself does not create a tuple, since parentheses must be usable for grouping of expressions). An empty tuple can be formed by an empty pair of parentheses.
I’m sure that the use of “singleton” here is wrong.
It does not say that ()
is a singleton.
Where did you see the claim for ()
being a singleton?
I would say different. The same word is used to name different concepts. The use there is as in the (earlier? than singleton pattern) use in Mathematics for sets of one element.
Using the same word for different concepts happens all the time.
Perhaps people should qualify the word further in places, like here and the docs, where multiple uses occur simultaneously: “singleton pattern”, “singleton object” (single instance of a class implementing the singleton pattern), “singleton container”, “singleton (mathematical) set”.