472,974 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,974 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 1251
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Fred | last post by:
Why was the bool type introduced into c++? What does it provide that int does not and are the two entirely interchangable in conditional expressions? Thanks Fred
13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
14
by: Joseph Turian | last post by:
How can I determine the type of some particular typename? I am writing a template, and it needs special case handling for some particular types: template <typename T> class foo { public:...
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
1
by: Thi | last post by:
Hi, I am trying to develop an application that allows the users to drag a few file(s) from a zip archive to a destination. My question is, how do i determine where the drop destination is...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
57
by: Alan Isaac | last post by:
Is there any discussion of having real booleans in Python 3000? Say something along the line of the numpy implementation for arrays of type 'bool'? Hoping the bool type will be fixed will be...
5
by: GiJeet | last post by:
Hello, I know that all that’s required to use linq to objects, is the collection must implement IEnumerable<Tbut how to know if a collection implements this interface? For example one of the books...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.