python3 type()_isinstance().py

"""
模块:type()_isinstance() 函数。
参考:https://www.cnblogs.com/wushuaishuai/p/7739728.html
知识点:
1.type() 函数,用于查看数据类型。
2.type()函数,不认为子类的实例和父类是一个类型。
isinstance()函数,认为子类的实例也是父类的实例。
"""
# 0.帮助
# help(type)

# 1.查看数据类型
print(type(1))
# <class 'int'>
print(type('runoob'))
# <class 'str'>
print(type([2]))
# <class 'list'>
print(type({0: 'zero'}))
# <class 'dict'>

# 2.判断数据类型。
print("2.")
print(type(1) == int)
# True
print(type('ok') == str)
# True
print(type([1]) == list)
# True
print(type({'a': 1}) == dict)
# True
print(type((1,)) == tuple)
# True
print(type({1}) == set)


# True

# 3.type vs isinstance()


class A:
    pass


class B(A):
    pass


print("3.")
print(isinstance(A(), A))
# True
print(type(A()) == A)
# True
print(isinstance(B(), A))
# True
print(type(B()) == A)
# False
发布了198 篇原创文章 · 获赞 58 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weixin_42193179/article/details/103970357