函数displayNumType()接受一个数值参数,使用内建函数type()来确认数值的类型(或不是一个数值类型)。
#!/usr/bin/env python
import sys
sys.setrecursionlimit(1000000)
def displayNumType(num):
print num, 'is',
if isinstance(num, (int, long, float, complex)):
print 'a number of type:', type(num)._name_
else:
print 'not a number at all!'
displayNumType(-69)
displayNumType(99999999999L)
displayNumType(98.6)
displayNumType(-5.2+1.9j)
displayNumType('xxx')