Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Data model - Subclass Vs Instance

For user-defined type X,

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

Q) How X behaves as both sub class and instance of object?


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

Q) How int behave as both sub class and instance of object?


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

Q) How type can be both, sub class and instance of object?


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

object not being sub class but instance of type, which makes sense

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

int not being sub class but instance of type, makes sense.

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

also makes sense.

Edit:

here it says, The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model.

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

Q) What is meta-model?

Q) What does it mean to say, type instance of type?

like image 797
overexchange Avatar asked Dec 19 '22 07:12

overexchange


1 Answers

All three of your questions come down to "how can X be both a subclass and instance of object". The answer is simple: everything is a subclass of object. Classes are objects (=instances of object), and hence, subclasses (including subclasses of object) are also instances of object.

like image 82
acdr Avatar answered Dec 21 '22 11:12

acdr