首页 > 其他 > 详细

isinstance type issubclass

时间:2018-12-27 22:15:34      阅读:167      评论:0      收藏:0      [点我收藏+]
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))

  

isinstance type issubclass

原文:https://www.cnblogs.com/work14/p/10187497.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!