Introduction: Python object() Function
When working with Python, there are situations where you need a simple object that does not contain any predefined data or behavior. Whether you’re learning object-oriented programming, creating placeholder objects, or understanding Python’s class hierarchy, having a basic object can be useful.
Without a built-in solution, you would need to define your own class even when you only require a plain object, adding unnecessary code to your program.
A simple solution to these situations is the Python object() Function.
What it is: The object() function is a built-in Python function that creates a new, featureless object. It is also the base class from which all Python classes ultimately inherit, making it a fundamental part of Python’s object-oriented programming model.
Take a look at a quick example to see how it works.
You can also explore its real-world use cases to learn where it is commonly used.
Let’s explore the syntax, parameters, return value, and practical examples of the object() function.
💡 Tip: The object() function is one of the built-in functions that helps Python perform efficient dictionary and set operations. Explore the
Python Built-in Functions Learning Guide to learn more useful functions with practical examples.
Syntax, Parameters, Return Value and Examples: Python object() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python object() Function.
Syntax
object()
Parameters
| Parameter | Description |
|---|---|
| None | The object() function does not accept any arguments. |
Return Value
| Return Value | Description |
|---|---|
object |
Returns a new base object instance. |
Quick Example
The following example creates a simple object.
obj = object()
print(obj)
# Sample Output:
<object object at 0x...>
The object() function creates a new object instance. Python displays its type along with a memory address, which differs every time the program runs.
How the Python object() Function Works
- The
object()function creates a new base object. - It does not accept any arguments.
- The created object does not contain custom attributes or methods.
- Every call to
object()creates a separate object instance. - It serves as the root of Python’s class inheritance hierarchy.
Examples: Python object() Function
The following examples show how the Python object() Function works in different programming scenarios.
Example 1: Creating a Simple Object
obj = object()
print(obj)
# Sample Output:
<object object at 0x...>
Explanation: The object() function creates a new base object. The memory address shown in the output is generated automatically and will be different each time the program runs.
Example 2: Checking the Type of an Object
obj = object()
print(type(obj))
# Output:
<class 'object'>
Explanation: Instead of displaying the object itself, this example checks its type. Python confirms that the created instance belongs to the built-in object class.
Example 3: Comparing Two Objects
obj1 = object()
obj2 = object()
print(obj1 == obj2)
# Output:
False
Explanation: Every call to object() creates a separate object. Since obj1 and obj2 refer to different instances, the comparison returns False.
Example 4: Returning an Object from a Function
def create_object():
return object()
result = create_object()
print(type(result))
# Output:
<class 'object'>
Explanation: A function can create and return a new object whenever it is called. The returned value is still an instance of Python’s built-in object class.
Example 5: Using object() as a Unique Placeholder
NOT_FOUND = object()
value = NOT_FOUND
print(value is NOT_FOUND)
# Output:
True
Explanation: Developers sometimes create a unique object as a special marker or placeholder. Since every object created by object() is unique, it can safely represent a special value.
Example 6: Passing an Object to a Function
def show(value):
print(type(value))
obj = object()
show(obj)
# Output:
<class 'object'>
Explanation: Like other Python objects, an object created with object() can be passed to functions as an argument and used wherever an object is expected.
Example 7: Showing that User-defined Classes Inherit from object
class Student:
pass
print(isinstance(Student(), object))
# Output:
True
Explanation: Even though the class does not explicitly inherit from object, Python automatically makes every user-defined class inherit from it. That is why isinstance() returns True.
Use Cases: When to use the object() Function
Below are some common situations where the Python object() Function becomes useful:
- Creating a simple base object.
- Learning Python’s object-oriented programming model.
- Creating unique placeholder or sentinel objects.
- Understanding Python’s inheritance hierarchy.
- Returning generic objects from functions.
- Building advanced libraries and frameworks.
Key Takeaways: object() Function
Before wrapping up, here are the key points to remember about the Python object() Function:
- The
object()function creates a new base object. - It does not accept any arguments.
- Every call creates a unique object instance.
- The returned object does not contain custom attributes or methods.
- The built-in
objectclass is the root of Python’s inheritance hierarchy. - It is commonly used to create unique placeholder objects and to understand object-oriented programming concepts.
In short, the Python object() Function provides a simple way to create base objects while helping developers understand Python’s object-oriented foundation and inheritance model.