+ 4

Why False is an instance of integer?

Here is the code: print(False in (0, 1)) print(isinstance(False, int)) outputs: True True https://code.sololearn.com/c9NlnwDTw75k/?ref=app

15th Aug 2021, 1:34 PM
Dolan
Dolan - avatar
8 Respuestas
+ 3
Usually in programming, True is symbolized by the number 1 and False is symbolized by the number 0.
15th Aug 2021, 1:53 PM
Yahel
Yahel - avatar
+ 2
Exactly. Here, because boolean is a subclass of int. So, False is 0. Then isinstance(False, int) == isinstance(0, int). Check: issubclass(bool, int) in #Python documentations.
15th Aug 2021, 1:54 PM
Dolan
Dolan - avatar
+ 2
Thank you. Exactly.
15th Aug 2021, 1:56 PM
Dolan
Dolan - avatar
+ 2
Originally, python had no boolean type, so 0 and 1 where used as False and True When they finally introduced boolens they needed to make them an instance of int for retro-compatibility As for today, there really isn't any need for it, so it's there just for an historic reason
15th Aug 2021, 1:57 PM
Angelo
Angelo - avatar
15th Aug 2021, 1:55 PM
PresidentOfYes12
PresidentOfYes12 - avatar
+ 1
Because it's one bit of one byte so it's either 1 or zero this is how integers are saved just with more bytes
15th Aug 2021, 2:30 PM
Abs Sh
Abs Sh - avatar
0
Thank you.
15th Aug 2021, 1:57 PM
Dolan
Dolan - avatar