Why Everything in Python Is an Object: A Deep Dive with Code Examples
This article explains Python's core philosophy that every entity—whether a basic data type, function, class, module, or collection—is an object, illustrating each concept with clear code snippets and showing how to inspect types and attributes using type() and dir().
Basic Data Types as Objects
In Python, fundamental types such as int, float, and str are objects. Each object has a type and a set of methods that can be inspected with type() and dir().
a = 5
print(type(a)) # output: <class 'int'>
print(dir(a)) # list of integer methods b = 3.14
print(type(b)) # output: <class 'float'>
print(dir(b)) # list of float methods c = "Hello, world!"
print(type(c)) # output: <class 'str'>
print(dir(c)) # list of string methodsFunctions as Objects
Functions are first‑class objects; they can be assigned to variables, passed as arguments, or returned from other functions.
def greet(name):
return f"Hello, {name}!"
greeting = greet
print(greeting("Alice")) # Hello, Alice! def apply_function(func, arg):
return func(arg)
def square(x):
return x * x
result = apply_function(square, 5)
print(result) # 25 def make_greeter(prefix):
def greeter(name):
return f"{prefix}, {name}!"
return greeter
greet_with_hello = make_greeter("Hello")
print(greet_with_hello("Alice")) # Hello, Alice!Classes and Instances as Objects
Classes themselves are objects, and instances inherit attributes and methods defined in the class.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
person = Person("Alice", 25)
print(person.greet()) # Hello, my name is Alice and I am 25 years old. print(type(Person)) # output: <class 'type'>
print(dir(Person)) # list of class attributes and methodsModules as Objects
Modules are objects that can contain variables, functions, and classes.
import math
print(math.sqrt(16)) # 4.0 print(type(math)) # output: <class 'module'>
print(dir(math)) # list of module attributes and functionsTuples, Lists, and Dictionaries as Objects
t = (1, 2, 3)
print(type(t)) # output: <class 'tuple'>
print(dir(t)) # ['count', 'index'] l = [1, 2, 3]
print(type(l)) # output: <class 'list'>
print(dir(l)) # list methods such as append, pop, sort, etc. d = {"name": "Alice", "age": 25}
print(type(d)) # output: <class 'dict'>
print(dir(d)) # dict methods such as get, keys, items, etc.Other Object Types
Class methods, static methods, descriptors, and metaclasses are also objects with special behaviors.
class MyClass:
@classmethod
def class_method(cls):
return "This is a class method."
@staticmethod
def static_method():
return "This is a static method."
print(MyClass.class_method()) # This is a class method.
print(MyClass.static_method()) # This is a static method. class Descriptor:
def __get__(self, instance, owner):
return "Value from descriptor."
class MyClass:
my_attr = Descriptor()
obj = MyClass()
print(obj.my_attr) # Value from descriptor. class Meta(type):
def __new__(cls, name, bases, attrs):
attrs["class_name"] = name
return super().__new__(cls, name, bases, attrs)
class MyClass(metaclass=Meta):
pass
obj = MyClass()
print(obj.class_name) # MyClassConclusion
Python treats almost everything as an object—basic types, functions, classes, modules, collections, and even metaclasses—allowing dynamic inspection and manipulation via type() and dir(). This uniform object model provides the language with great flexibility and powerful object‑oriented capabilities.
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.
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.
