如何确定字符串和整数之间的bool? [英] How to determine the bool between the strings and ints?

查看:66
本文介绍了如何确定字符串和整数之间的bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一本设置字典。 settinfgs可以是字符串,整数

或bools。我想将这个列表动态地写入磁盘中的一个很大的

for循环,不幸的是bool需要写入0或1到带有WriteInt的

配置,整数也用WriteInt和字符串

用简单的写。


列表是这样的;


options [A] = True

options [b] = 1

options [C] =" Hello"


I想使用isinstance来确定它是bool还是int或

字符串。但是我很困惑在交互式编辑器中尝试它;


>> a =错误
if isinstance(a,bool):



.... print" OK"

....

OK


> ;> if isinstance(a,int):



.... print" OK"

....

OK


>> ;>



我收不到。是以某种方式从''int'派生的bool?什么是

检查我想写的配置是int还是

bool的最好方法是什么?


问候,

- Jorgen

解决方案

Jorgen Boddeaécrit:


大家好,


我有一本设置字典。 settinfgs可以是字符串,整数

或bools。我想将这个列表动态地写入磁盘中的一个很大的

for循环,不幸的是bool需要写入0或1到带有WriteInt的

配置,整数也用WriteInt和字符串

用简单的写。


列表是这样的;


options [A] = True

options [b] = 1

options [C] =" Hello"


I想使用isinstance来确定它是bool还是int或

字符串。但是我很困惑在交互式编辑器中尝试它;


>>> a = False
如果isinstance(a,bool):



... print" OK"

...

OK


>>> if isinstance(a,int):



...打印OK

...

OK


我不明白。是以某种方式从''int'派生的bool?



显然:是的! - )


什么是

最好检查我想写的配置是int还是

bool的方法?


>> isinstance(0,bool)



False


>> isinstance(1,bool)



False


>>>



但无论如何,我不明白这一点,因为需要写出bool

为0或1的配置与WriteInt,整数也与WriteInt。

所以你只是不在乎它是否是一个bool?或者我错过了什么?


9月7日上午8:40,Jorgen Bodde < jorgen.maill ... @ gmail.comwrote:


大家好,


我有一个设置字典。 settinfgs可以是字符串,整数

或bools。我想将这个列表动态地写入磁盘中的一个很大的

for循环,不幸的是bool需要写入0或1到带有WriteInt的

配置,整数也用WriteInt和字符串

用简单的写。


列表是这样的;


options [A] = True

options [b] = 1

options [C] =" Hello"


I想使用isinstance来确定它是bool还是int或

字符串。但是我很困惑在交互式编辑器中尝试它;


> a = False
if isinstance(a, bool):



...打印OK

...

OK>> if isinstance(a,int):


...打印OK

...

好的


我收不到。是以某种方式从''int'派生的bool?什么是

检查我想写的配置是int还是

bool的最好方法是什么?


问候,

- Jorgen



这是在过去两周的讨论中提出但我不能找到
找到它在搜索谷歌。


看来你可以得到确切的类(而不是这是

类或衍生物那个isinstance()似乎提供了具有__class__属性的



>> a = True
打印.__ class__ == bool



True


>> print a .__ class__ == int



False


>> b = 1
print b .__ class__ == int



True


>> print b .__ class__ == bool



False



棒极了!谢谢你!


至于为什么关心他们是否是bool,我写的是真实和假的

属性,内部机制就像这样工作所以我需要

来区分。


再次感谢大家,

- Jorgen


ps。对不起TheFlyingDutch亲自邮寄给​​你,我一直忘了

这个邮件列表在回复时没有默认回用户列表

;-)


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

解决方案

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 ?


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



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


这篇关于如何确定字符串和整数之间的bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆