Python - Data Model - Subclass Vs Instance

For custom type X,

>>> class X(object):
...     pass
... 
>>> issubclass(X, object)    # User-defined type
True
>>> isinstance(X, object)    # User-defined type
True

Q) How Xdoes it behave as a subclass and instance object?


>>> issubclass(int, object)  # Built-in type
True
>>> isinstance(int, object)  # Built-in type
True

Q) How do intthey behave as a subclass and instance object?


>>> issubclass(type, object)  # Meta class
True
>>> isinstance(type, object)  # Meta class
True

Q) How typecan it be both a subclass and an instance object?


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

objectnot a subclass, but an instance type, which makes sense

>>> issubclass(int, type)
False
>>> isinstance(int, type)
True
>>> 

intnot a subclass, but an instance typemakes sense.

>>> issubclass(X, type)      # User-defined type
False
>>> isinstance(X, type)      # User-defined type
True

also makes sense.

Edit:

here , the message said. The main motivation for introducing new style classes is to provide a single object model with a complete metamodel.

Q) How to understand the meaning of a unified object model?

Q) What is a metamodel?

Q) , type type?

+4
3

:

enter image description here

, A.__class__ == B, B in A.__bases__. python , type .

,

  • issubclass(x, y) " x, , y"

  • isinstance(x, y) " x, , issubclass"


: :

class MetaClass(type): pass
class MClass(metaclass=MetaClass): pass
class Class(object): pass
c = MClass()

enter image description here source

+4

" X , object". : object. - (= object), , , ( object) object.

+5

Python - , isinstance(whatever, object) == True - . . ( Py3k ), type object, , , Int type ( object), . - python object, , . - mro, . , objectprovides a set of convenient descriptors that are called as default values ​​for tons of other types (since types ultimately inherit most of the functionality from object).

+1
source

Source: https://habr.com/ru/post/1678418/


All Articles