From f365271df3363fc503ba59c33fd7157caa860a8c Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Sun, 9 Jun 2019 00:07:02 +0530 Subject: [PATCH] New example: The chicken-egg problem Closes https://github.com/satwikkansal/wtfpython/issues/101 --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.md b/README.md index 888a676..6777d95 100644 --- a/README.md +++ b/README.md @@ -731,6 +731,54 @@ Even when the values of `x` were different in every iteration prior to appending --- +### ▶ The chicken-egg problem ^ + +1\. +```py +>>> isinstance(3, int) +True +>>> isinstance(type, object) +True +>>> isinstance(object, type) +True +``` + +2\. So which is the ultimate, base class? And wait, there's more to the confusion + +```py +>>> class A: pass +>>> isinstance(A, A) +False +>>> isinstance(type, type) +True +>>> isinstance(object, object) +True +``` + +3\. + +```py +>>> issubclass(int, object) +True +>>> issubclass(type, object) +True +>>> issubclass(object, type) +False +``` + + +#### 💡 Explanation + +- `type` is a [metaclass](https://realpython.com/python-metaclasses/) in Python. +- **Everything** is an `object` in Python, which includes classes as well as their objects (instances). +- class `type` is the metaclass of class `object`, and every class (including `type`) has inherited directly or indirectly from `object`. +- There is no real base class among `object` and `type`. The confusion in the above snippets is arising because we're thinking these relationships (`issubclass` and `isinstance`) in terms of Python classes. The relationship between `object` and `type` can't be reproduced in pure python. To be more precise the following relationships can't be reproduced in pure Python, + + class A is instance of class B, and class B is an instance of class A. + + class A is an instance of itself. +- These relationships between `object` and `type` (both being instances of eachother as well as themselves) exist in Python because of "cheating" at implementation level. + +--- + ### ▶ `is not ...` is not `is (not ...)` ```py