This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bool variable isinstance of int
Type: behavior Stage: resolved
Components: IO Versions: Python 3.6
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, noobie1000, steven.daprano
Priority: normal Keywords:

Created on 2021-11-24 13:04 by noobie1000, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg406926 - (view) Author: (noobie1000) Date: 2021-11-24 13:04
Hello,

Recently I observed that isinstance(x, int) returns True, even when x is defined as a bool. While I understand that internally, a bool is treated as an int with values 0 and 1; to me, this is a bit misleading that the python interpreter returns True when we perform the isinstance() check. May be I'm missing some deeper explanation. Could someone please shed some light on this. Has this been discussed by the community in the past. Thank you very much for reading this ticket.

>>> x = True
>>> isinstance(x, int)
True
msg406929 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-11-24 13:47
This is the expected behavior. The bool type is a subtype of int. True is int 1 in disguise and False is int 0.

>>> bool.__mro__
(<class 'bool'>, <class 'int'>, <class 'object'>)

>>> True == 1
True
>>> True == 2
False

>>> False == 0
True
msg406932 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-11-24 14:18
Hi noobie1000, 

This is not a general forum for asking questions about Python's design or language, this is for reporting bugs.

There are many forums where you can ask "Why" questions and ask for the community to "shed some light on this", such as Reddit's r/python, the various mailing lists, Discuss, IRC, etc. In future please use them rather than the bug tracker.

Regarding bool, you can start by reading this:

https://www.python.org/dev/peps/pep-0285/

If that doesn't answer your questions, please feel free to take them to the many different forums here:

https://www.python.org/community/forums/

https://www.python.org/community/lists/

https://www.python.org/community/irc/
msg406933 - (view) Author: (noobie1000) Date: 2021-11-24 14:28
Hello Steven,

Sorry, this is my first ever post and was lost in the enormity of issues/documentation. Noted your points :)

Thank you.
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90049
2021-11-24 14:30:23noobie1000setstatus: open -> closed
2021-11-24 14:28:48noobie1000setstatus: closed -> open
resolution: not a bug ->
messages: + msg406933
2021-11-24 14:18:47steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg406932

resolution: not a bug
stage: resolved
2021-11-24 13:47:37christian.heimessetnosy: + christian.heimes
messages: + msg406929
2021-11-24 13:04:10noobie1000create