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))
    

      

  • 相关阅读:
    最大流问题的几种经典解法综述
    有上下界的网络流
    hiho一下
    poj 1018
    状压dp
    hdu 1043
    Poj1015
    7.14
    sgu 128
    (zhuan)
  • 原文地址:https://www.cnblogs.com/YangWenYu-6/p/10187624.html
Copyright © 2011-2022 走看看