Skip to content

Commit

Permalink
New example: The chicken-egg problem
Browse files Browse the repository at this point in the history
  • Loading branch information
satwikkansal committed Jun 8, 2019
1 parent 0f06974 commit 217d3ac
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,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
Expand Down

0 comments on commit 217d3ac

Please sign in to comment.