473,320 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1260
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.