分享

怎么判断数据类型

 融水公子 2018-09-11
-----------------------------------------------------------------------------------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a,b,c,d=20,5.5,True,4+3j
print(type(a),type(b),type(c),type(d))
#(<type 'int'>, <type 'float'>, <type 'bool'>, <type 'complex'>)
----------------------------------------------------------------------------------------
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print(100,type(100))  #(100, <type 'int'>)
print('100',type('100'))       #('100', <type 'str'>)
----------------------------------------------------------------------------------------
此外还可以用 isinstance 来判断:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = 111
print(isinstance(a, int))#True
----------------------------------------------------------------------------------------
综合:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class A:
    pass

class B(A):
    pass
print(isinstance(A(), A))    # returns True
print(type(A()) == A )      # returns False
print(isinstance(B(), A))    # returns True
print(type(B()) == A)       # returns False
               



分享知识,分享快乐!希望中国站在编程之巅!

               ----

公众微信号:rsgz520

360图书馆馆号:rsgz002.360doc.com

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约