Why Everything in Python Is an Object: Unraveling Types, Instances, and Metaclasses
This article explains Python’s core principle that everything is an object, detailing the distinction between type objects, instance objects, and metaclasses, how built‑in types like int and str fit into the hierarchy, the role of the object base class, variable naming as pointers, and the differences between mutable, immutable, fixed‑length and variable‑length objects.
Everything in Python Is an Object
In Python, everything—integers, strings, dictionaries, even user‑defined classes—is an object. Built‑in types such as int, str, and list are type objects; when instantiated they become instance objects.
Both type objects and instance objects are themselves objects, so the object‑oriented model is implemented entirely through objects.
Type objects represent classes; for example, int is a class (a type object) and 123 is an instance of that class. The metaclass type is the type of all type objects, including itself, forming a self‑referential hierarchy.
All objects ultimately inherit from the built‑in object class, which is the base of the inheritance chain.
Type and Object Hierarchy
An integer variable a = 123 has type int and isinstance(a, int) returns True. The type of int is type, and type itself is an instance of type, making it a metaclass.
>>> int
<class 'int'>
>>> type(int)
<class 'type'>Custom classes behave the same way:
class Female:
pass
print(type(Female)) # <class 'type'>
print(issubclass(Female, object)) # TrueInheritance can be inspected via __base__, __bases__, and __mro__:
class A: pass
class B: pass
class C(A): pass
class D(B, C): pass
print(D.__base__) # <class '__main__.B'>
print(D.__bases__) # (<class '__main__.B'>, <class '__main__.C'>)
print(D.__mro__) # (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)Variables Are Names (Pointers)
In Python a variable stores a reference (pointer) to an object, not the object itself. Assigning a new value creates a new object and rebinds the name to that object.
# C example
int a = 123;
printf("%p", &a); // same address after reassignment
# Python example
a = 666
print(hex(id(a))) # e.g. 0x1b1333394f0
a = 667
print(hex(id(a))) # different addressWhen two names refer to the same object they share the same id:
a = 666
b = a
print(id(a) == id(b)) # TrueMutable vs. Immutable Objects
Immutable objects (e.g., integers, strings, tuples) cannot be modified in place; operations that appear to change them actually create new objects, resulting in a different id.
a = 666
print(id(a))
a += 1
print(id(a)) # differentMutable objects (e.g., lists, dictionaries) can be changed without altering their id:
lst = [1, 2, 3]
print(id(lst))
lst.append(4)
print(id(lst)) # sameFixed‑Length vs. Variable‑Length Objects
Objects whose memory footprint varies with their value (e.g., large integers) are called variable‑length objects; objects with a constant size (e.g., floats) are fixed‑length.
import sys
print(sys.getsizeof(0)) # 24
print(sys.getsizeof(1 << 33)) # 32 (larger integer)
print(sys.getsizeof(3.14)) # 24 (float, fixed size)Strings also have variable size; an empty string still occupies a base amount of memory for bookkeeping.
import sys
print(sys.getsizeof("")) # 49
print(sys.getsizeof("a")) # 50
print(sys.getsizeof("abc")) # 52Summary
Python’s object system treats everything as an object, with three roles: instance objects, type objects, and metaclasses. All types inherit from object, while all objects have type as their ultimate type. Variables are merely names bound to object references, and understanding mutability, memory layout, and inheritance is essential for mastering Python’s dynamic behavior.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
