How to determine the bool between the strings and ints?

J

Jorgen Bodde

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 = 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;
.... print "OK"
....
OK.... 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
 
B

Bruno Desthuilliers

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 = 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;
... print "OK"
...
OK... 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 ?

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 ?
 
T

TheFlyingDutchman

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 = 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;

... 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:

False
 
J

Jorgen Bodde

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
;-)
 
M

Marc 'BlackJack' Rintsch

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
 
Z

Zentrader

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..... if type(x) == type(1):
.... print "int"
.... else:
.... print "not int"
....
not int

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

Marc 'BlackJack' Rintsch

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
 
S

Steven D'Aprano

is the bool derived from 'int' in some way?
Yes.
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"
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Zentrader said:
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.... if type(x) == type(1):
... print "int"
... else:
... print "not int"
...
not int

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

Or just :
True
 
S

Steven D'Aprano

Or just :

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.
 
G

Guest

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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top