isinstance: 判断你给对象是否是xx类型的. (向上判断)
type: 返回xxx对象的数据类型
issubclass: 判断xxx类是否xxx的子类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Animal:
    def eat(self):
        print("刚睡醒吃点儿东西")
 
class Cat(Animal):
    def play(self):
        print("猫喜欢玩儿")
 
= Cat()
 
 
 
print(isinstance(c, Cat)) # c是一只猫
print(isinstance(c, Animal)) # 向上判断
 
= Animal()
print(isinstance(a, Cat)) # 不能向下判断
 
 
print(type(a)) # 返回 a的数据类型
print(type([]))
print(type(c)) # 精准的告诉你这个对象的数据类型
 
# 判断.xx类是否是xxxx类的子类
print(issubclass(Cat, Animal))
print(issubclass(Animal, Cat))
 
# 应用
def cul(a, b): # 此函数用来计算数字a和数字b的相加的结果
    # 判断传递进来的对象必须是数字. int float
    if (type(a) == int or type(a) == floatand (type(b) == int or type(b) == float):
        return + b
    else:
        print("对不起. 您提供的数据无法进行计算")
 
print(cul(a, c))