This is second and final part in this series: Python has no variables.

Part 1: https://deeppython.org/Blog-Python-Has-No-Variables-Part1.html
Continuing from Part 1, let us further explore the classes of different data structures in python.
In [3]:
[1,2].__class__
Out[3]:
list
In [4]:
{"a": 1, "b": 2}.__class__
Out[4]:
dict
In [5]:
5.668.__class__
Out[5]:
float

As we mentioned earlier, everthing in Python is object, the types float, dict, list etc are subclasses of object. Let us see this:

In [6]:
import inspect #inspect module helps to get the heirarchy (method resoultion order) of the resolution of objects
inspect.getmro(type(5.668))
Out[6]:
(float, object)

Above, we could see that float type is subclass of object class. Other way to see this is to find on what class the subclass type float is based and what order they are resolved.

In [11]:
print(5.668.__class__.__bases__)
print(5.668.__class__.__mro__)
(<class 'object'>,)
(<class 'float'>, <class 'object'>)

Here is another interesting one. The bool type. We know it represents True or False, it is based on type int and int is based on object. Let us see that:

In [18]:
print(1==1)
print((1==1).__class__)
print((1==1).__class__.__bases__)
print((1==1).__class__.__bases__[0].__bases__)
True
<class 'bool'>
(<class 'int'>,)
(<class 'object'>,)

True, False, bool are names that are created by python.

In [25]:
print(True.__class__)
print(True.__class__.__bases__)
print(True.__class__.__bases__[0].__bases__)
<class 'bool'>
(<class 'int'>,)
(<class 'object'>,)

More on this:

In [30]:
print(True.__class__ == (1==1).__class__)
print(True.__class__ is (1==1).__class__)
## the ids are same
print(id(True.__class__), id((1==1).__class__))
True
True
4500258304 4500258304

What about built-in functions? class? type? object?

In [34]:
print(len)
print(len.__class__)
print(len.__class__.__mro__)
<built-in function len>
<class 'builtin_function_or_method'>
(<class 'builtin_function_or_method'>, <class 'object'>)

Built-in functions are objects too and thay have their class as 'builtin_function_or_method' which is subclass of 'object'.

Recap: Everthing is object. Every object has value, type, base classes, unique id, and attributes. We can create more objects by calling other callable objects.

Reference: Stuart Williams - Python Epiphanies - PyCon 2018 https://www.youtube.com/watch?v=-kqZtZj4Ky0&t=498s