SoFunction
Updated on 2025-04-26

An article will help you understand objects in Python in depth

What is object()?

object()is a built-in function in Python, which is used to create the most basic object instance. In Python, all classes are inherited fromobject, even if you don't explicitly inherit it. therefore,objectIt is the base class of all Python objects. It defines some basic behaviors and attributes that will be inherited by all objects.
When you create a class, Python automatically uses it as a base class even if you don't write it inherited from an object:

class MyClass:
    pass

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

The role and characteristics of object()

object is the base class of all classes

In Python, object is the base class of all classes, that is, the root of all classes. If you define a new class without specifying it to inherit from a class, it will inherit from object by default

class MyClass:
    pass

print(issubclass(MyClass, object))  # Output:True

Basic and immutable objects

Objects created with object() are the simplest objects in Python. They have no properties or methods except for some basic behavior (such asstrrepreqwait). Additionally, an instance of object is immutable, meaning you cannot add properties to the object.

obj = object()
 = "example"  # Will report an error:AttributeError: 'object' object has no attribute 'name'

object provides basic object behavior

As a base class for all objects, object provides some basic methods, such as:

  • str(): Returns the string representation of the object (default is the object's memory address).
  • repr(): Returns the official string representation of the object.
  • eq(other): determine whether two objects are equal.
  • ne(other): determine whether two objects are not equal.
  • hash(): Returns the hash value of the object so that the object can be used as a key for the dictionary.
class MyClass:
    def __str__(self):
        return "This is MyClass object."

obj = MyClass()
print(str(obj))  # Output:This is MyClass object.

object() as placeholder

In Python programming, object() can also be used as a placeholder. This usage is very useful in function parameters, conditional judgments, or other scenarios that require unique identification. Since each call to object() generates a new, unique object instance, this makes it a good placeholder or glyph.

NOT_SET = object()

def my_func(value=NOT_SET):
    if value is NOT_SET:
        print("Value is not set.")
    else:
        print(f"Value is {value}")

my_func()  # Output: Value is not set.my_func(10)  # Output:Value is 10

Why understand object?

Although we rarely use object() directly in daily programming, it is still crucial to understand its importance in Python. object is the base class of all classes, and it provides the basic behavior for all objects. Understanding this helps us better understand Python's object-oriented system.

In addition, object() can also be used in special scenarios such as placeholders or unique identifiers, and such designs can be very helpful when writing more robust and flexible code.

Summarize

This is all about this article about object() in Python. For more related object() content in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!