如何检查我的python对象是否是数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4187185/
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
How can I check if my python object is a number?
提问by Neal Ehardt
In Java the numeric types all descend from Number so I would use
在 Java 中,数字类型都从 Number 下降,所以我会使用
(x instanceof Number).
What is the python equivalent?
python的等价物是什么?
采纳答案by Matt
Test if your variable is an instance of numbers.Number:
测试您的变量是否是以下项的实例numbers.Number:
>>> import numbers
>>> import decimal
>>> [isinstance(x, numbers.Number) for x in (0, 0.0, 0j, decimal.Decimal(0))]
[True, True, True, True]
This uses ABCsand will work for all built-in number-like classes, and also for all third-party classes if they are worth their salt (registered as subclasses of the NumberABC).
这使用ABC并将适用于所有内置数字类,也适用于所有第三方类,如果它们值得他们的盐(注册为NumberABC 的子类)。
However, in many cases you shouldn't worry about checking types manually - Python is duck typedand mixing somewhat compatible types usually works, yet it will barf an error message when some operation doesn't make sense (4 - "1"), so manually checking this is rarely really needed. It's just a bonus. You can add it when finishing a module to avoid pestering others with implementation details.
但是,在许多情况下,您不必担心手动检查类型——Python 是鸭子类型的,混合一些兼容的类型通常是可行的,但是当某些操作没有意义时,它会发出错误消息 ( 4 - "1"),因此手动检查这是很少真正需要。这只是一个奖励。您可以在完成一个模块时添加它,以避免用实现细节纠缠其他人。
This works starting with Python 2.6. On older versions you're pretty much limited to checking for a few hardcoded types.
这从 Python 2.6 开始工作。在旧版本上,您几乎只能检查一些硬编码类型。
回答by Falmarri
That's not really how python works. Just use it like you would a number, and if someone passes you something that's not a number, fail. It's the programmer's responsibility to pass in the correct types.
这不是python的真正工作方式。就像使用数字一样使用它,如果有人传递给您的不是数字的东西,请失败。传递正确的类型是程序员的责任。
回答by Matt
Python 2:
isinstance(x, (int, long, float, complex)) and not isinstance(x, bool)
蟒蛇2:
isinstance(x, (int, long, float, complex)) and not isinstance(x, bool)
Python 3:
isinstance(x, (int, float, complex)) and not isinstance(x, bool)
蟒蛇3:
isinstance(x, (int, float, complex)) and not isinstance(x, bool)
回答by user225312
Sure you can use isinstance, but be aware that this is not how Python works. Python is a duck typed language. You should not explicitly check your types. A TypeErrorwill be raised if the incorrect type was passed.
当然您可以使用isinstance,但请注意,这不是 Python 的工作方式。Python 是一种鸭子类型语言。您不应该明确检查您的类型。TypeError如果传递了不正确的类型,则会引发A。
So just assume it is an int. Don't bother checking.
所以假设它是一个int. 不要费心检查。
回答by Steven Rumbalski
Use Numberfrom the numbersmodule to test isinstance(n, Number)(available since 2.6).
Number从numbers模块中使用以进行测试isinstance(n, Number)(自 2.6 起可用)。
isinstance(n, numbers.Number)
Here it is in action with various kinds of numbers and one non-number:
这里它与各种数字和一个非数字一起工作:
>>> from numbers import Number
... from decimal import Decimal
... from fractions import Fraction
... for n in [2, 2.0, Decimal('2.0'), complex(2,0), Fraction(2,1), '2']:
... print '%15s %s' % (n.__repr__(), isinstance(n, Number))
2 True
2.0 True
Decimal('2.0') True
(2+0j) True
Fraction(2, 1) True
'2' False
This is, of course, contrary to duck typing. If you are more concerned about how an object acts rather than what it is, perform your operations as if you have a number and use exceptions to tell you otherwise.
当然,这与鸭子类型相反。如果您更关心一个对象的行为方式而不是它是什么,请像您有一个数字一样执行您的操作,并使用异常来告诉您其他情况。

