python --- 19 判断对象所属,区分函数和对象, 反射

一.判断对象所属

  isinstance, type , issubclass

  1.issubclass(x,y)    判断x是否是y 的子类

    

  2.type(x)  精准返回x 的数据类型

  3.isinstance(x,y) 判断x是否是y类型的数据

    只能向上判断

二.区分函数和方法

  1.打印变量名有  function   就是函数

        有  method  就是方法

   2.

  ①.实例方法 

    访问时    类名.方法      是函数

              对象.方法     是方法

  ② 类方法   都是方法

  ③静态方法   都是函数

       3.其他:

  用模块 :from types import MethodType, FunctionType

      isinstance(x,MethodType)        判断是否是方法

      isinstance(x,FunctionType)        判断是否是函数

三.反射

  1.hasattr(对象,功能)

    判断对象有没有这个功能

  2.getattr(对象,功能)

    得到这个功能

  3.setattr(对象,属性,新增)

    属性存在时改变旧值

            不存在时新增

  4.delattr(对象,属性)

      把属性移除

原文地址:https://www.cnblogs.com/sc-1067178406/p/10152243.html