Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bool variable isinstance of int #90049

Closed
noobie1000 mannequin opened this issue Nov 24, 2021 · 4 comments
Closed

bool variable isinstance of int #90049

noobie1000 mannequin opened this issue Nov 24, 2021 · 4 comments
Labels
topic-IO type-bug An unexpected behavior, bug, or error

Comments

@noobie1000
Copy link
Mannequin

noobie1000 mannequin commented Nov 24, 2021

BPO 45891
Nosy @tiran, @stevendaprano

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-11-24.14:30:23.502>
created_at = <Date 2021-11-24.13:04:10.797>
labels = ['type-bug', 'expert-IO']
title = 'bool variable isinstance of int'
updated_at = <Date 2021-11-24.14:30:23.502>
user = 'https://bugs.python.org/noobie1000'

bugs.python.org fields:

activity = <Date 2021-11-24.14:30:23.502>
actor = 'noobie1000'
assignee = 'none'
closed = True
closed_date = <Date 2021-11-24.14:30:23.502>
closer = 'noobie1000'
components = ['IO']
creation = <Date 2021-11-24.13:04:10.797>
creator = 'noobie1000'
dependencies = []
files = []
hgrepos = []
issue_num = 45891
keywords = []
message_count = 4.0
messages = ['406926', '406929', '406932', '406933']
nosy_count = 3.0
nosy_names = ['christian.heimes', 'steven.daprano', 'noobie1000']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45891'
versions = ['Python 3.6']

@noobie1000
Copy link
Mannequin Author

noobie1000 mannequin commented Nov 24, 2021

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

@noobie1000 noobie1000 mannequin added topic-IO type-bug An unexpected behavior, bug, or error labels Nov 24, 2021
@tiran
Copy link
Member

tiran commented Nov 24, 2021

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

@stevendaprano
Copy link
Member

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/

@noobie1000
Copy link
Mannequin Author

noobie1000 mannequin commented Nov 24, 2021

Hello Steven,

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

Thank you.

@noobie1000 noobie1000 mannequin reopened this Nov 24, 2021
@noobie1000 noobie1000 mannequin removed the invalid label Nov 24, 2021
@noobie1000 noobie1000 mannequin reopened this Nov 24, 2021
@noobie1000 noobie1000 mannequin removed the invalid label Nov 24, 2021
@noobie1000 noobie1000 mannequin closed this as completed Nov 24, 2021
@noobie1000 noobie1000 mannequin closed this as completed Nov 24, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants