【问题标题】:The instance relation between type and object in python?python中类型和对象的实例关系?
【发布时间】:2021-12-19 23:32:32
【问题描述】:

显示类型和对象之间的类关系:

issubclass(type,object)
True
issubclass(object,type)
False

很明显type派生自objectobjecttype的父类,typeobject的儿子class

isinstance(type,object)
True
isinstance(object,type)
True

如何理解typeobject 的实例,反之亦然?

【问题讨论】:

    标签: python-3.x object types instance


    【解决方案1】:

    有用的定义

    isinstance(对象,类信息)

    如果 object 参数是 classinfo 参数或其(直接、间接或虚拟)子类的实例,则返回 True
    https://docs.python.org/3.9/library/functions.html?highlight=isinstance#isinstance

    1) typeobject 的一个实例

    • 所有数据都由对象表示,object 是所有类的基础

    对象是 Python 对数据的抽象。 Python 程序中的所有数据都由对象或对象之间的关系表示。
    https://docs.python.org/3.9/reference/datamodel.html#types

    object 是所有类的基础。
    https://docs.python.org/3.9/library/functions.html?highlight=object#object

    2) objecttype 的一个实例

    • object 是一个类型对象。换句话说,object 的类型为 type

    类型对象
    类型对象表示各种对象类型。通过内置函数type()
    https://docs.python.org/3.9/library/stdtypes.html?highlight=subclass#type-objects

    访问对象的类型
    • type(object) # returns type

    • type(type) # returns type

    • object.__class__ # returns type

    • type.__class__ # returns type

    • 这有助于我们了解isInstance(object, type) 将如何返回True

    • 一种直观的思考方式是 object 对象的类型为 typeobject 不是 type 的子类,因为它不继承自 @987654351 @

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多