471,350 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

How to determine the bool between the strings and ints?

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
Sep 7 '07 #1
10 1180
Jorgen Bodde a écrit :
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?
Obviously : yes !-)
What is
the best way to check if the config I want to write is an int or a
bool ?
>>isinstance(0, bool)
False
>>isinstance(1, bool)
False
>>>
But anyway, I don't get the point, since "the bools need to be written
as 0 or 1 to the config with WriteInt, the integers also with WriteInt".
So you just don't care if it's a bool or not ? Or did I miss something ?
Sep 7 '07 #2
On Sep 7, 8:40 am, "Jorgen Bodde" <jorgen.maill...@gmail.comwrote:
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


Sep 7 '07 #3
Awesome! Thanks you!

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.

Thanks again guys,
- Jorgen

ps. Sorry TheFlyingDutch for mailing you personally, I keep forgetting
this mailinglist does not default back to the user list when replying
;-)
Sep 7 '07 #4
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
Sep 7 '07 #5
On Sep 7, 11:30 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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

Sep 7 '07 #6
On Fri, 07 Sep 2007 12:26:46 -0700, Zentrader wrote:
Looks like a "feature" of isinstance() is to consider both True and 1 as
booleans, but type() distinguishes between the two.
That's not a "feature", it is just OOP. `bool` is a subclass of `int`
therefore every `bool` instance is also an instance of `int`. There's
nothing special about it.

In [57]: issubclass(bool, int)
Out[57]: True

In [58]: bool.__base__
Out[58]: <type 'int'>

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #7
On Fri, 07 Sep 2007 17:40:44 +0200, Jorgen Bodde wrote:
is the bool derived from 'int' in some way?
Yes.
>>issubclass(bool, int)
True

What is the best way to
check if the config I want to write is an int or a bool ?
if isinstance(value, bool):
print "it's a bool, or a subclass of bool"
elif isinstance(value, int):
print "it's an int, or a subclass of int other than bool"

--
Steven.
Sep 8 '07 #8
Zentrader wrote:
On Sep 7, 11:30 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>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
Sep 8 '07 #9
On Sat, 08 Sep 2007 12:08:16 -0300, Ricardo Aráoz wrote:

> if type(x) == type(True):
... print "bool"
...
bool

Or just :
>>>a = True
type(a) == int
False
[snip]

You know, one or two examples was probably plenty. The other six or seven
didn't add anything to your post except length.

Also, type testing by equality is generally not a good idea. For example:

class HexInt(int):
"""Like built-in ints, but print in hex by default."""
def __str__(self):
return hex(self)
__repr__ = __str__

You should be able to use a HexInt anywhere you can use an int. But not
if your code includes something like this:

if type(value) == int:
do_something()
else:
print "Not an int!"

(What do you mean my HexInt is not an int? Of course it is.)

Better is to use isinstance(value, int). Better still is to do duck-
typing, and avoid type() and isinstance() as much as possible.

--
Steven.
Sep 8 '07 #10
Steven D'Aprano wrote:
....
...
..
You know, one or two examples was probably plenty. The other six or seven
didn't add anything to your post except length.

Also, type testing by equality is generally not a good idea. For example:

class HexInt(int):
"""Like built-in ints, but print in hex by default."""
def __str__(self):
return hex(self)
__repr__ = __str__

You should be able to use a HexInt anywhere you can use an int. But not
if your code includes something like this:

if type(value) == int:
do_something()
else:
print "Not an int!"

(What do you mean my HexInt is not an int? Of course it is.)

Better is to use isinstance(value, int). Better still is to do duck-
typing, and avoid type() and isinstance() as much as possible.
>>type(a) == HexInt
True

That's what I wanted (though I don't know if that's what the OP wanted).
BTW, sorry for the wasted bandwidth, didn't realize you might have such
a small bandwidth that seven lines would be a hassle. We should also
tell the blokes of the 'music' thread to stop it, I can imagine how mad
that must get you.

Sep 8 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Fred | last post: by
14 posts views Thread by Joseph Turian | last post: by
25 posts views Thread by lovecreatesbeauty | last post: by
7 posts views Thread by semedao | last post: by
57 posts views Thread by Alan Isaac | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.