From: MRAB on
mk wrote:
> >>> isinstance(False, int)
> True
> >>>
> >>> isinstance(True, int)
> True
>
> Huh?
>
> >>>
> >>> issubclass(bool, int)
> True
>
> Huh?!
>
Python didn't have Booleans originally, 0 and 1 were used instead. When
bool was introduced it was made a subclass of int so that existing code
wouldn't break.
From: mk on
Arnaud Delobelle wrote:

>>>> 1 == True
> True
>>>> 0 == False
> True
>
> So what's your question?

Well nothing I'm just kind of bewildered: I'd expect smth like that in
Perl, but not in Python.. Although I can understand the rationale after
skimming PEP 285, I still don't like it very much.


Regards,
mk


From: Rolando Espinoza La Fuente on
On Fri, Mar 5, 2010 at 2:32 PM, mk <mrkafk(a)gmail.com> wrote:
> Arnaud Delobelle wrote:
>
>>>>> 1 == True
>>
>> True
>>>>>
>>>>> 0 == False
>>
>> True
>>
>> So what's your question?
>
> Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl,
> but not in Python.. Although I can understand the rationale after skimming
> PEP 285, I still don't like it very much.
>

So, the pythonic way to check for True/False should be:

>>> 1 is True
False

>>> 0 is False
False

instead of ==, right?

Regards,

Rolando
From: mk on
Rolando Espinoza La Fuente wrote:

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

It does, in fact I was wondering why my iterator didn't work until I
figured issubclass(bool, int) is true.

Regards,
mk

From: Steven D'Aprano on
On Fri, 05 Mar 2010 15:01:23 -0400, Rolando Espinoza La Fuente wrote:

> On Fri, Mar 5, 2010 at 2:32 PM, mk <mrkafk(a)gmail.com> wrote:
>> Arnaud Delobelle wrote:
>>
>>>>>> 1 == True
>>>
>>> True
>>>>>>
>>>>>> 0 == False
>>>
>>> True
>>>
>>> So what's your question?
>>
>> Well nothing I'm just kind of bewildered: I'd expect smth like that in
>> Perl, but not in Python.. Although I can understand the rationale after
>> skimming PEP 285, I still don't like it very much.
>>
>>
> So, the pythonic way to check for True/False should be:
>
>>>> 1 is True
> False

Why do you need to check for True/False?

But if you need to, yes, that is one way. Another would be:

isinstance(flag, bool)

But generally, you can use any object as a flag without caring if it is
actually True or False.




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