mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-12-22 21:00:09 +01:00
New example: The chicken-egg problem
Closes https://github.com/satwikkansal/wtfpython/issues/101
This commit is contained in:
parent
9b3b367b45
commit
f365271df3
48
README.md
vendored
48
README.md
vendored
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user