Fundamentals 7 min read

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().

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Why Everything in Python Is an Object: A Deep Dive with Code Examples

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 methods

Functions 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 methods

Modules 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 functions

Tuples, 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)  # MyClass

Conclusion

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData TypesclassesfunctionsfundamentalsObject-OrientedModules
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.