In Python, the object()
function is a built-in function that returns a new featureless object. It is the base class for all Python classes, and every class in Python implicitly or explicitly inherits from it.
Here's the basic syntax of the object()
function:
object()
When you create a new class in Python without specifying a base class, it automatically becomes a subclass of object
. For example:
class MyClass:
pass
print(isinstance(MyClass(), object)) # Output: True
Even though object
itself doesn't have any special attributes or methods, it forms the foundation of the Python class hierarchy. All built-in and user-defined classes ultimately derive from object
.
In Python 2.x, classes that don't explicitly inherit from object
are considered old-style classes, while classes that do inherit from object
are considered new-style classes. However, starting from Python 3, all classes are new-style classes, and explicit inheritance from object
is not required (though it doesn't hurt to include it for clarity and compatibility with older code).
For instance, in Python 3, the following class definitions are equivalent:
# Python 3
class MyClass:
pass
class MyClass(object):
pass
In most cases, you don't need to use the object()
function explicitly. It's primarily there to represent the base class for all classes in Python. Instead, you'll typically create classes without mentioning object
at all, like the examples provided above.