Are True and False singletons? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Are True and False singletons?

"Most literals are compared by equality, however the singletons True, False and None are compared by identity." -- https://peps.python.org/pep-0636/#appendix-a-quick-intro How can True and False be called singletons when they both have the same type, <class 'bool'>, and "singleton" means the only instance of a class that can only have one instance? print(type(True)) # <class 'bool'> print(type(False)) # <class 'bool'>

22nd Feb 2024, 6:02 AM
Rain
Rain - avatar
9 Antworten
+ 4
Rain I think in the context it is not referring to the object design pattern. Rather, I believe it is implying True, False and None each have one memory location that is statically assigned. FYI, here is how ChatGPT explains it: "In Python, "singleton" refers to the property of having only one instance within the runtime of a program, not necessarily within a type. So, True and False are singletons because there's only one instance of each within the Python interpreter's runtime, even though they share the same type, bool."
22nd Feb 2024, 7:13 AM
Brian
Brian - avatar
+ 2
Maybe because bool is a subclass of int? https://docs.python.org/3/c-api/bool.html
22nd Feb 2024, 2:08 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Rain I guess you already found out integer ranging from -5 to 256 are preallocated integers as describe in this link. https://inventwithpython.com/beyond/chapter9.html#calibre_link-182 At first, I entirely don't understand your question, and the online translator gave me a not so meaningful result as it translate "singleton" literally. However, I guess the word came from "single". And then from your code in the second post you demonstrated how python (more specific, cpython) allocate integer value, which remains me the article I linked. At that point, I believe "singleton" can be described as "unique", and may have something more in programming. Then I search "what is the meaning of singleton in programming" and this link come at the top. https://refactoring.guru/design-patterns/singleton It says, "Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance." It is when I realize it is a question about design pattern, and "Singleton" is one of those. This description apply to True and False, as they have a single and unique ID living in the program, which you also demonstrated in the comments in your previous code. Meanwhile, as bool is a subclass of int, and 0 and 1 falls into the preallocated integers, it all makes sense to me. I think the designers of the interpreter take advantage of the int preallocated integers. Thanks to you, I learned something new. However, I wouldn't care much about it because a programming language is a tool to me, unless you want to be a programming language / interpreter inventor or maintainer.
23rd Feb 2024, 5:20 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Just adding this to the thread for the record and to save any reader some programming time who wants to confirm the behavior real quick. https://sololearn.com/compiler-playground/cA2V5E7425iL/?ref=app
24th Feb 2024, 4:11 AM
Rain
Rain - avatar
+ 1
Brian , That's interesting, but ChatGPT isn't a source, and you can't ask it, "Where did you read that?" There's already a term for values that have one runtime instance (one id(), one memory location), "interned" values, as with the int values from -5 to 256. I'm thinking since the docs use the term singleton for True, False, None, Ellipsis, and NotImplemented, but not for those ints, singleton must have another connotation, possibly including interned plus additional details. Otherwise they would just call them interned. Note that though some int values are interned, there's no limit on int values, but bool can only have two. You can't create a third bool value for example. So there has to be more to it. I'm craving a citable definition. https://sololearn.com/compiler-playground/c86oah6P46Ca/?ref=app
22nd Feb 2024, 1:24 PM
Rain
Rain - avatar
+ 1
I followed up with ChatGPT (though I agree, it is not a source) and explored the differences between interned objects and the intrinsic constant objects. [ To refresh your memory, here is the reference for the intrinsic constant objects: https://docs.python.org/3/library/constants.html ] This is my take from ChatGPT's answers: Their purposes are different and they are stored in different locations. Objects that are interned are placed there only for memory optimization. The intrinsic objects have a semantic purpose as well, besides optimization. That means they are intertwined with the Python interpreter logic, and its internal control flow is affected by them. In contrast, interned objects are mere data items that are manipulated, but have no further semantic meaning to program execution. Sorry, no citable reference. It's all just implicit in the necessary computer science behind interpreters and compilers.
22nd Feb 2024, 2:57 PM
Brian
Brian - avatar
2nd Mar 2024, 10:52 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
Wong Hei Ming , Please elaborate.
22nd Feb 2024, 5:56 PM
Rain
Rain - avatar
0
[I haven't forgotten. I'm researching. Will post later.]
24th Feb 2024, 2:48 AM
Rain
Rain - avatar