Python Forum
Why is bool() an instance of int()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is bool() an instance of int()?
#1
I was surprised to find out that isinstance(bool(1), int) returns True. Why would that be?

If you have the following code:
def a(*args):
   if len(args) != 3:
       return False
   return True

def b(*args):
   return errno.EINVAL

func = <dynamically assign one of a() or b()>
ret = func()
How would you check if either of the returned a failure?
Reply
#2
(Jul-11-2025, 06:25 PM)voidtrance Wrote: Why would that be?


check PEP-285 written by Guido:
Quote:Should bool inherit from int?

=> Yes.

In an ideal world, bool might be better implemented as a separate integer type that knows how to perform mixed-mode arithmetic. However, inheriting bool from int eases the implementation enormously (in part since all C code that calls PyInt_Check() will continue to work – this returns true for subclasses of int). Also, I believe this is right in terms of substitutability: code that requires an int can be fed a bool and it will behave the same as 0 or 1. Code that requires a bool may not work when it is given an int; for example, 3 & 4 is 0, but both 3 and 4 are true when considered as truth values.


Also check https://stackoverflow.com/q/8169001/4046632
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-11-2025, 06:44 PM)buran Wrote: Also check https://stackoverflow.com/q/8169001/4046632

The explanation there does not make sense to me. "... True and False masquerade as 1 and 0, respectively." is perfectly fine. The issue is that the reality is the opposite - 0 and 1 are masquerading as False and True. I always though that bool was a different type than int not just an "alias".

But, I guess, it is what it is. The decision has been made a long time ago and now we have to live with it.
Reply
#4
(Jul-11-2025, 07:06 PM)voidtrance Wrote: I always though that bool was a different type than int not just an "alias".
bool is not an alias of int, it is a subclass
>>> bool.__mro__
(<class 'bool'>, <class 'int'>, <class 'object'>)
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
Quote:How would you check if either of the returned a failure?
failed = retval in (errno.EINVAL, False)
Better yet, raise an exception when you have an error. Status return values are for languages without good exception handling support.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,632 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  Bool Object is not Subscriptable quest 1 6,200 May-02-2021, 11:12 AM
Last Post: Yoriz
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() Rejoice 6 5,623 Aug-25-2020, 03:50 PM
Last Post: Rejoice
  bool b = (num == 100) this doesn't work? MelonMusk 2 3,280 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item() bongielondy 2 17,187 Nov-27-2019, 03:25 PM
Last Post: ThomasL
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 6,728 Dec-03-2018, 11:16 PM
Last Post: Windspar
  bool PreservedKillich 6 5,226 Aug-01-2018, 03:09 AM
Last Post: Skaperen
  How to check if class instance exists in a list of class instance objects? sonicblind 23 30,230 May-27-2018, 05:44 AM
Last Post: buran
  [split] Set bool variable RedSkeleton007 1 3,937 Oct-22-2017, 09:28 PM
Last Post: metulburr
  Python3x running only with one instance (how can I prevent too many running instance) harun2525 5 23,450 Jul-21-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020