检查对象是否为数字 [英] check if object is number
问题描述
有没有一种好方法可以确定一个对象是否是一个数字类型?
一般来说,我避免使用类型检查来支持try / except块,但我是
不知道在这种情况下该怎么办:
def f(i):
...
如果x<我:
...
问题是,如果''我'是一个字符串,则不会抛出任何错误:
py> 1< ''a''
True
py> 10000000000< ''a''
正确
但是对于我的代码,传递字符串很糟糕,所以我想提供一个
适当的错误。
我想在值上调用int(),但是这也会允许
一些字符串(例如''1 '')。我想这并不可怕,但似乎有点不理想......
想法?
Steve
Is there a good way to determine if an object is a numeric type?
Generally, I avoid type-checks in favor of try/except blocks, but I''m
not sure what to do in this case:
def f(i):
...
if x < i:
...
The problem is, no error will be thrown if ''i'' is, say, a string:
py> 1 < ''a''
True
py> 10000000000 < ''a''
True
But for my code, passing a string is bad, so I''d like to provide an
appropriate error.
I thought about calling int() on the value, but this will also allow
some strings (e.g. ''1''). I guess this isn''t horrible, but it seems
somewhat suboptimal...
Ideas?
Steve
推荐答案
Steven Bethard写道:
Steven Bethard wrote:
有没有一种很好的方法来确定对象是否是数字类型?
一般来说,我避免使用类型检查来支持try / except块,但是我不知道在这种情况下该怎么做:
def f(i) :
...
如果x<我:
问题是,如果''我'是一个字符串,不会抛出任何错误:
py> 1< ''a''
真的
py> 10000000000< ''''
但对于我的代码,传递一个字符串很糟糕,所以我想提供一个
适当的错误。
<我想过在值上调用int(),但是这也会允许一些字符串(例如1)。我想这并不可怕,但似乎有点不理想......
Is there a good way to determine if an object is a numeric type?
Generally, I avoid type-checks in favor of try/except blocks, but I''m not sure what to do in this case:
def f(i):
...
if x < i:
...
The problem is, no error will be thrown if ''i'' is, say, a string:
py> 1 < ''a''
True
py> 10000000000 < ''a''
True
But for my code, passing a string is bad, so I''d like to provide an
appropriate error.
I thought about calling int() on the value, but this will also allow
some strings (e.g. ''1''). I guess this isn''t horrible, but it seems
somewhat suboptimal...
这个怎么样?
.... def is_number(x):
....尝试:
.... x + 1
....返回True
....除了TypeError:
....返回False
How about this?
.... def is_number(x):
.... try:
.... x + 1
.... return True
.... except TypeError:
.... return False
2005年2月11日星期五12:11:44 -0700,Steven Bethard
< st ************ @ gmail.com>写道:
On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard
<st************@gmail.com> wrote:
有没有一种很好的方法来确定一个对象是否是一个数字类型?
一般来说,我避免使用类型检查来支持try / except块,但我是<不知道在这种情况下该怎么做:
def f(i):
...
如果x<我:
问题是,如果''我'是一个字符串,不会抛出任何错误:
py> 1< ''a''
真的
py> 10000000000< ''''
但是对于我的代码,传递字符串很糟糕,所以我想提供一个适当的错误。
怎么样 :
如果类型(变量)== type(1):
print"是一个整数>
else:
print"请输入一个整数
我考虑过调用int()值,但这也会允许一些字符串(例如''1'')。我想这并不可怕,但似乎有点不理想......
想法?
史蒂夫
-
http://mail.python.org/mailman/listinfo/python-清单
Is there a good way to determine if an object is a numeric type?
Generally, I avoid type-checks in favor of try/except blocks, but I''m
not sure what to do in this case:
def f(i):
...
if x < i:
...
The problem is, no error will be thrown if ''i'' is, say, a string:
py> 1 < ''a''
True
py> 10000000000 < ''a''
True
But for my code, passing a string is bad, so I''d like to provide an
appropriate error.
How about:
if type(variable) == type(1):
print "is an integer"
else:
print "please input an integer"
I thought about calling int() on the value, but this will also allow
some strings (e.g. ''1''). I guess this isn''t horrible, but it seems
somewhat suboptimal...
Ideas?
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Bill Mill写道:
Bill Mill wrote:
2005年2月11日星期五12:11:44 -0700, Steven Bethard
< st ************ @ gmail.com>写道:
On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard
<st************@gmail.com> wrote:
有没有一个很好的方法来确定一个对象是否是一个数字类型?
Is there a good way to determine if an object is a numeric type?
如何 :
如果类型(变量)== type(1):
print"是一个整数
else:
print"请输入一个整数
How about:
if type(variable) == type(1):
print "is an integer"
else:
print "please input an integer"
这会检查它是否为整数,而不是数字类型:
py> x = 1
py> type(x)== type(1)
True
py> #using int而不是type(1)
py> type(x)== int
True
py> x = 1.0
py> type(x)== int
False
Steve
This checks if it is an integer, not if it is a numeric type:
py> x = 1
py> type(x) == type(1)
True
py> # using int instead of type(1)
py> type(x) == int
True
py> x = 1.0
py> type(x) == int
False
Steve
这篇关于检查对象是否为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!