isinstance type issubclass

class Animal:
    def eat(self):
        print("刚睡醒吃点儿东西")

class Cat(Animal):
    def play(self):
        print("猫喜欢玩儿")

c = Cat()



print(isinstance(c, Cat)) # c是一只猫
print(isinstance(c, Animal)) # 向上判断

a = 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) == float) and (type(b) == int or type(b) == float):
        return a + b
    else:
        print("对不起. 您提供的数据无法进行计算")

print(cul(a, c))

  

posted on 2018-12-27 21:28  =.===  阅读(98)  评论(0编辑  收藏  举报

导航