Python 检查对象是数字还是布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15019830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:07:33  来源:igfitidea点击:

Check if object is a number or boolean

pythonpython-2.7

提问by Joakim

Design a logical expression equivalent to the following statement:

xis a list of three or five elements, the second element of which is the string 'Hip'and the first of which is not a number or Boolean.

设计一个与以下语句等效的逻辑表达式:

x是一个包含三个或五个元素的列表,其中第二个元素是字符串'Hip',第一个元素不是数字或布尔值。

What I have:

我拥有的:

x = ['Head', 'Hip', 10]
print x[1] is 'Hip'

My question: How do you check for whether or not it is a Boolean or a number?

我的问题:你如何检查它是布尔值还是数字?

采纳答案by Lev Levitsky

To answer the specific question:

要回答具体问题:

isinstance(x[0], (int, float))

This checks if x[0]is an instance of any of the types in the tuple (int, float).

这将检查是否x[0]是元组中任何类型的实例(int, float)

You can add boolin there, too, but it's not necessary, because boolis itself a subclass of int.

您也可以bool在那里添加,但这不是必需的,因为bool它本身就是int.

Doc reference:

文档参考:



To comment on your current code, you shouldn't rely on interning of short strings. You are supposed to compare strings with the ==operator:

要评论您当前的代码,您不应该依赖于短字符串的实习。您应该将字符串与==运算符进行比较:

x[1] == 'Hip'

回答by Peyman Karimi

import types
type(x) == types.BooleanType

回答by CurlyMo

Easiest i would say:

最简单的我会说:

type(x) == type(True)

回答by Thijs Cobben

In python3 this would be: type(x)==boolsee example.

在 python3 中,这将是:type(x)==bool参见示例

回答by Justin Golden

You should compare the type of xto the boolclass:

您应该将 的类型xbool类进行比较:

type(x) == bool

or:

或者:

type(x) == type(True)

Here is more on the type method

这里有更多关于类型方法的信息

From Data model docs:

来自数据模型文档

Booleans (bool)

These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.

布尔值 (bool)

这些代表真值 False 和 True。表示值 False 和 True 的两个对象是唯一的布尔对象。布尔类型是整数类型的子类型,布尔值的行为分别类似于值 0 和 1,几乎在所有上下文中,例外是转换为字符串时,返回字符串“False”或“True” , 分别。

回答by Michael

I follow the recent answer who tell to use typeand it seems to be the incorrect way according to pylintvalidation:

我遵循了最近的答案 who tell to use type,根据pylint验证,这似乎是不正确的方法:

I got the message:

我收到消息:

C0123: Using type() instead of isinstance() for a typecheck. (unidiomatic-typecheck)

C0123:使用 type() 而不是 isinstance() 进行类型检查。(单语类型检查)

Even if it's an old answer, the correct one is the accepted answer of @Lev Levitsky:

即使这是一个旧答案,正确的答案是@Lev Levitsky 已接受的答案:

isinstance(x[0], (int, float))