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

问题描述

大家好,


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

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

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

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

用简单的写。


列表是这样的;


options [A] = True

options [b] = 1

options [C] =" Hello"


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

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



.... print" OK"

....

OK



.... print" OK"

....

OK



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

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

bool的最好方法是什么?


问候,

- Jorgen

解决方案



... print" OK"

...

OK



...打印OK

...

OK


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



显然:是的! - )



False



False



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

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

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




...打印OK

...

OK>> if isinstance(a,int):


...打印OK

...

好的


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

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

bool的最好方法是什么?


问候,

- Jorgen



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


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

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




True



False



True



False




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;

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

解决方案

... print "OK"
...
OK

... print "OK"
...
OK

I don''t get it. is the bool derived from ''int'' in some way?

Obviously : yes !-)

False

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 ?



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

True

False

True

False




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

10-11 04:17