I will be a strange voice (since all answers condemn the use of the fact False == 0 and True == 1 , as the language guarantees), since I argue that using this fact to simplify the code is excellent.
Historically, logical operations true / false usually use 0 for false and 1 for true; During the Python 2.2 life cycle, Guido noticed that too many modules started with jobs, such as false = 0; true = 1 false = 0; true = 1 , and this led to the creation of a template and useless variation (the latter due to the fact that the capitalization of true and false was everywhere - some used all-caps, some all-lowercase, some cap-initial) and therefore introduced a subclass of bool int and its constants True and False .
There was quite a bit of response at that time, since many of us were afraid that the new type and constants would be used by Python beginners to limit language possibilities, but Guido was adamant that we were just pessimistic: no one would ever understand Python so badly, for example, to avoid the natural use of False and True as indexes on a list or in summation or other such perfectly clear and useful idioms.
The answers to this question prove that we were right: as we feared, there was a complete misunderstanding of roles of this type and constants, and people avoid and, even worse, convince others to avoid completely natural Python constructs in favor of useless vibrations.
Struggling with the excitement of such a misunderstanding, I urge everyone to use Python as Python, not , trying to drive it into the form of other languages, the functionality and preferred style of which are completely different. In Python, True and False are 99.9%, like 1 and 0, they differ only in the form of str(...) (and thus repr(...) ) - for any operation other than string, just feel free to use them without distortion. This applies to indexing, arithmetic, bit operations, etc. Etc. Etc.
Alex Martelli Jul 04 2018-10-04T00: 00Z
source share