Python object() Function

Spread the love

The object() function is a built-in function in Python that, at its core, returns a new featureless object. object is the base for all classes; it holds the built-in properties and methods which are default for all Python objects.


The syntax of the object() function is

obj = object()

This syntax returns a new object instance that is essentially empty—it has no instance attributes, and it can’t be customized further since it doesn’t allow setting any new attributes.


The object() function doesn’t accept any parameters.

Return Value:

The object() function returns a new object without any features—it’s a base object that doesn’t contain any methods or properties except for those common to all Python objects.

The Role of object() in Python’s OOP

The Base Class

In Python’s OOP, every class implicitly derives from object, making it the most base type. This means that every object in Python inherits methods from the object class, such as:

  • __str__(): Defines behavior for when str() is called, as in string representation of the object.
  • __repr__(): Defines behavior for when repr() is called, providing the object’s official string representation.
  • __eq__(): Defines behavior for equality comparison (==).
  • __hash__(): Returns the hash value of an object.
  • __bool__(): Defines boolean value of an object when it is used in a boolean context.

The Minimal Object

Creating an object via object() gives you the most stripped-down, minimal piece of Python data:

minimal = object()

print(type(minimal))  # Output: <class 'object'>
print(dir(minimal))   # Lists all the attributes and methods inherited from the object base class

Using object() in Practice

While you might not frequently create instances of object directly, understanding its functionality is key to comprehending Python’s OOP system.

Custom Objects

class MyObject:

obj = MyObject()
print(isinstance(obj, object))  # Output: True

This example illustrates that instances of even user-defined classes are instances of object, and therefore, inherit the base methods.


When designing functions that can accept any type of object, using object as a parameter type denotes that literally any Python object can be passed to the function.


Using object() instances can act as placeholders for data that is not meant to be manipulated or accessed.

Advanced Topics Related to object()

Overriding Magic Methods

Python classes can override the magic methods inherited from object to provide meaningful functionality:

class CustomClass:
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return f"Custom Object with value: {self.value}"
    def __repr__(self):
        return f"CustomClass({self.value})"

my_object = CustomClass(10)
print(my_object)  # Output: Custom Object with value: 10
print(str(my_object))  # Output: Custom Object with value: 10
print(repr(my_object))  # Output: CustomClass(10)


object plays a crucial role in metaclasses, which are classes of classes. All metaclasses derive from the type class, which is itself an instance of object.

MyMeta = type('MyMeta', (object,), {})

Here, MyMeta is a new metaclass that has object as its base class.

Real-World Applications

While the direct instantiation of object() is rare, its existence underpins several common real-world applications:

1. Testing and Mocking

In unit tests, object() instances can be used as a simple mock object.

2. Object-oriented Design

Understanding object is fundamental to designing Python classes effectively, adhering to principles like inheritance and polymorphism.

3. API Design

Frameworks and libraries often use objects as a base for extensibility. Knowing how object() works allows developers to better understand and leverage these designs.


The object() function may seem inconsequential due to its simplicity and the fact that it’s often overshadowed by more complex classes and functions. However, it is a fundamental aspect of Python, representing the base from which all is built in Python’s OOP landscape. It’s a pristine example of Python’s minimalist design and a testament to the language’s commitment to clarity and simplicity.

Leave a Reply