object() is one of Python's built-in functions that returns a new object instance. object is the base class for all classes, meaning all Python classes directly or indirectly inherit from the object class.
class MyClass(object): # Explicitly inherit from object
pass
class MyClass: # Implicitly inherit from object
pass
The object class is the base class for all classes:
class MyClass:
pass
# MyClass inherits from the object class
print(isinstance(MyClass, object)) # Output: True
object instances cannot have attributes added, but subclasses of object can:
obj = object()
# obj.x = 123 # AttributeError: 'object' object has no attribute 'x'
class MyObject(object):
pass
obj = MyObject()
obj.x = 123
print(obj.x) # 123
All built-in data types inherit from object:
print(isinstance(123.456, object)) # True
print(isinstance("hello", object)) # True
print(isinstance([], object)) # True
print(isinstance({}, object)) # True
print(isinstance((), object)) # True
object provides several special methods that all subclasses will inherit:
for name in dir(object()):
print(name)
Program Output
__class__ __delattr__ __dir__ __doc__ __eq__ __format__ __ge__ __getattribute__ __getstate__ __gt__ __hash__ __init__ __init_subclass__ __le__ __lt__ __ne__ __new__ __reduce__ __reduce_ex__ __repr__ __setattr__ __sizeof__ __str__ __subclasshook__