python内建函数type()

print type('')
s='xyd'
print type(s)
print type(199)
print type(0+0j)
print type(1+8j)
print type('long')
print type(0.0)
print type([])
print type(())
print type(type)
class Foo:pass
foo = Foo()
print type(Foo)
print type(foo)
class Bar(object):pass
bar = Bar()
print type(bar)
print type(Bar)
<type 'str'>
<type 'str'>
<type 'int'>
<type 'complex'>
<type 'complex'>
<type 'str'>
<type 'float'>
<type 'list'>
<type 'tuple'>
<type 'type'>
<type 'classobj'>
<type 'instance'>
<class '__main__.Bar'>
<type 'type'>
#------------------------isinstance()-----------------------------------------#
def displayNumType(num):
   print num,'is',
   if isinstance(num,(int,long,float,complex)):
      #isinstance()函数里面接受一个类对象作为参数
      print 'a number of type:',type(num)
   else:
      print 'not a number at all'
displayNumType(90.0)
displayNumType(2+9j)
displayNumType(23)

输出

90.0 is a number of type: <type 'float'>
(2+9j) is a number of type: <type 'complex'>
23 is a number of type: <type 'int'>
欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
原文地址:https://www.cnblogs.com/flyingcr/p/10428317.html