How to determine the bool between the strings and ints?

Ricardo Aráoz ricaraoz at gmail.com
Sat Sep 8 17:08:16 CEST 2007


Zentrader wrote:
> On Sep 7, 11:30 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>> On Fri, 07 Sep 2007 18:49:12 +0200, Jorgen Bodde wrote:
>>> As for why caring if they are bools or not, I write True and False to
>>> the properties, the internal mechanism works like this so I need to
>>> make that distinction.
>> Really?  Can't you just apply the `int()` function?
>>
>> In [52]: map(int, [1, 0, True, False])
>> Out[52]: [1, 0, 1, 0]
>>
>> Ciao,
>>         Marc 'BlackJack' Rintsch
> 
> Blackjack's solution would take care of the problem, so this is just
> for general info.  Looks like a "feature" of isinstance() is to
> consider both True and 1 as booleans, but type() distinguishes between
> the two.
>>>> x=True
> ... if type(x) == type(1):
> ...    print "int"
> ... else:
> ...    print "not int"
> ...
> not int
> 
>  if type(x) == type(True):
> ...    print "bool"
> ...
> bool
> 

Or just :

>>> a = True
>>> type(a) == int
False
>>> type(a) == bool
True
>>> a = 'True'
>>> type(a) == bool
False
>>> type(a) == str
True
>>> a = 5
>>> type(a) == bool
False
>>> type(a) == str
False
>>> type(a) == int
True
>>> a = 4.323
>>> type(a) == int
False
>>> type(a) == float
True



More information about the Python-list mailing list