check if object is number

Steven Bethard steven.bethard at gmail.com
Fri Feb 11 14:35:29 EST 2005


Bill Mill wrote:
> On Fri, 11 Feb 2005 12:11:44 -0700, Steven Bethard
> <steven.bethard at gmail.com> wrote:
> 
>>Is there a good way to determine if an object is a numeric type?
> 
> How about:
> if type(variable) == type(1):
>     print "is an integer"
> else:
>     print "please input an integer" 

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



More information about the Python-list mailing list