zoukankan      html  css  js  c++  java
  • isinstance, type, issubclass

    isinstance: 判断你给对象是否是xx类型的. (向上判断)
    type: 返回xxx对象的数据类型
    issubclass: 判断xxx类是否xxx的子类
    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))
    

      

  • 相关阅读:
    将变量名变为字符串
    sqlte3 的约束
    sqlte3 的基本使用4
    sqlite 的基本使用3
    sqlite 的基本使用2
    sqlite 的基本使用1
    TCP和UDP的区别(转)
    Mac 如何优雅的使用Microsoft office
    RGB和HSV颜色空间
    腾讯云视频开发相关参考
  • 原文地址:https://www.cnblogs.com/YangWenYu-6/p/10187624.html
Copyright © 2011-2022 走看看