Why in Python False==0 gives True and why is not used in the set function? That is, here is the line:

 print set([False, 0]) 

allows you to display:

 set([False]) 

Where did 0 go? All because == ?

By the way, such a line print set([1, True]) gives the result set([1]) . Why did True disappear this time, but 1 remained?

Are True and False just macros for 0 and 1? But why then True is 1 gives False ?

  • one
    True + 1 == 2. In fact, yes, aliases. But the types are different - FeroxTL
  • That is, if you look at the source of the interpreter, you can find something like #define True 1 ? - faoxis
  • you don’t even need to open, see the answer - FeroxTL

1 answer 1

I think it will become clearer if the parent classes bool derived:

 >>> True.__class__.__bases__ (<type 'int'>,) 

That is, in fact, yes, True and False are numbers. Respectively 1 and 0.

  • one
    This is in almost all programming languages. Well, besides java, there is a Boolean, but this is for aesthetes - KoVadim
  • one
    @faoxis exactly github.com/python/cpython/blob/… - FeroxTL
  • one
    The @faoxis bool type is just a descendant of an int type. You can create your child: class MyIntSubclass(int):pass . Aside: set uses hash, not equality - jfs
  • one
    @faoxis for the sake of interest, look at _Py_TrueStruct github.com/python/cpython/blob/… - it is announced immediately here - FeroxTL
  • one
    @faoxis is in no way contrary to my comment. - KoVadim