From: mk on
>>> isinstance(False, int)
True
>>>
>>> isinstance(True, int)
True

Huh?

>>>
>>> issubclass(bool, int)
True

Huh?!

Regards,
mk



From: Steve Holden on
mk wrote:
>>>> isinstance(False, int)
> True
>>>>
>>>> isinstance(True, int)
> True
>
> Huh?
>
>>>>
>>>> issubclass(bool, int)
> True
>
> Huh?!
>
>>> 3+True
4
>>> 3+False
3
>>>

Just a brainfart from the BDFL - he decided (around 2.2.3, IIRC) that it
would be a good ideal for Booleans to be a subclass of integers.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

From: Arnaud Delobelle on
mk <mrkafk(a)gmail.com> writes:

>>>> isinstance(False, int)
> True
>>>>
>>>> isinstance(True, int)
> True
>
> Huh?
>
>>>>
>>>> issubclass(bool, int)
> True
>
> Huh?!
>
> Regards,
> mk

Yes, and:

>>> True + False
1

In fact:

>>> 1 == True
True
>>> 0 == False
True

So what's your question?

--
Arnaud
From: Steven D'Aprano on
On Fri, 05 Mar 2010 18:14:16 +0100, mk wrote:

>>>> isinstance(False, int)
> True
> >>>
> >>> isinstance(True, int)
> True
>
> Huh?

Yes. Do you have an actual question?


> >>> issubclass(bool, int)
> True
>
> Huh?!

Exactly.

Bools are a late-comer to Python. For historical and implementation
reasons, they are a subclass of int, because it was normal for people to
use 0 and 1 as boolean flags, and so making False == 0 and True == 1 was
the least likely to break code.

E.g. back in the day, you would have something like:

{2:None}.has_key(2) -> 1

So folks would do:

print "The key is", ["missing", "present"][d.has_key(key)]

Which still works even now that has_key returns True or False rather than
1 or 0.


--
Steven
From: Rolando Espinoza La Fuente on
On Fri, Mar 5, 2010 at 2:00 PM, Steve Holden <steve(a)holdenweb.com> wrote:
[...]
>
> Just a brainfart from the BDFL - he decided (around 2.2.3, IIRC) that it
> would be a good ideal for Booleans to be a subclass of integers.
>

I would never figured out

>>> bool.__bases__
(<type 'int'>,)

Doesn't have side effects not knowing that False/True are ints?

Regards,

Rolando
 |  Next  |  Last
Pages: 1 2 3 4
Prev: python on a thumb drive?
Next: Escaping variable names