How to determine the bool between the strings and ints?

TheFlyingDutchman zzbbaadd at aol.com
Fri Sep 7 18:31:28 CEST 2007


On Sep 7, 8:40 am, "Jorgen Bodde" <jorgen.maill... at gmail.com> wrote:
> Hi All,
>
> I have a dictionary with settings. The settinfgs can be strings, ints
> or bools. I would like to write this list dynamically to disk in a big
> for loop, unfortunately the bools need to be written as 0 or 1 to the
> config with WriteInt, the integers also with WriteInt and the strings
> with a simple Write.
>
> The list is something like;
>
> options[A] = True
> options[B] = 1
> options[C] = "Hello"
>
> I wanted to use isinstance to determine if it is a bool or an int or a
> string. However I am confused trying it out in the interactive editor;
>
> >>> a = False
> >>> if isinstance(a, bool):
>
> ...     print "OK"
> ...
> OK>>> if isinstance(a, int):
>
> ...     print "OK"
> ...
> OK
>
>
>
> I don't get it. is the bool derived from 'int' in some way? What is
> the best way to check if the config I want to write is an int or a
> bool ?
>
> Regards,
> - Jorgen

This came up in a discussion within the last two weeks but I cannot
find it in a search of google.

It appear that you can get the exact class (as opposed to "is this
class or a derivative" that isinstance() seems to provide)
 with the  __class__ attribute:


>>> a = True
>>> print a.__class__ == bool
True
>>> print a.__class__ == int
False
>>> b = 1
>>> print b.__class__ == int
True
>>> print b.__class__  == bool
False







More information about the Python-list mailing list