1.Everything is an object
In Python, a number, a string, a function, a class, and a module are all objects — each has a type, an identity, and can carry attributes. There is no separate category of "primitive" value that behaves differently.
Key features
- type(x) works on any value at all — a number, a function, a class, a module
- A class is itself an object, of type type — which is why Name() can be called
- Functions carry attributes too: add.__name__, add.__doc__, add.__code__
- isinstance(5, object) is True — object sits at the root of every type
- Because functions are objects, they can be stored in lists, passed as arguments, returned
type() applied across Python's usual categories
- 5, "x", a function, a class — every one of these is an object
- type(x) — reports which class built it — always answers something
- isinstance(x, object) — always True — object is the root of every type
Command
type(5) # <class 'int'>
type(print) # <class 'builtin_function_or_method'>
type(int) # <class 'type'> — a class is an object too
isinstance(5, object) # True, for any value at alltype(x) answers for any value — a number, a function, or a class. isinstance(x, object) is always True. There is no non-object value in Python.