The Python object()
function is a built-in function that returns a new object of type object
. The object
type is the base type in Python and all other classes in Python are derived from it. It is an instance of the object class and is used to create an object that can be used as a placeholder for an object of any type.
Syntax
object()
Parameters
The object()
function takes no arguments.
Return Value
The object()
function returns a new object of type object
.
Examples
Example 1: Creating a new object of type object
obj = object() print(type(obj))
Output:
<class 'object'>
In this example, we have defined a variable obj
and assigned it the value returned by the object()
function. Then we have used the type()
function to check the type of the object. The output shows that the object is of type object
.
Example 2: Creating an object of a class derived from object
class MyClass(object): pass obj = MyClass() print(type(obj))
Output:
<class '__main__.MyClass'>
In this example, we have defined a class MyClass
that is derived from object
. Then we have defined a variable obj
and assigned it the value returned by instantiating the class MyClass
. Finally, we have used the type()
function to check the type of the object. The output shows that the object is of type MyClass
.
Example 3: Creating an object of a user-defined class
class MyClass: pass obj = MyClass() print(type(obj))
Output:
<class '__main__.MyClass'>
In this example, we have defined a class MyClass
that is not derived from object
. Then we have defined a variable obj
and assigned it the value returned by instantiating the class MyClass
. Finally, we have used the type()
function to check the type of the object. The output shows that the object is of type MyClass
Use Cases
The object()
function is used in various scenarios including:
- As a placeholder for an object of any type: The
object
type is the base type in Python, and all other classes are derived from it. It can be used as a placeholder for an object of any type. For example, when defining a class, it can be specified that the class should inherit fromobject
, even if it doesn’t need any of its methods. - For creating an instance of an object: The
object()
function can be used to create an instance of an object of typeobject
which can be used to create objects of other classes. For example, theMyClass
in Example 2 is derived fromobject
and an instance ofMyClass
is created usingobject()
. - To check the type of an object: The
type()
function can be used to check the type of an object. In all the examples above, we have used thetype()
function to check the type of the object created usingobject()
.
Conclusion
In conclusion, the Python object()
function is a simple but useful function that returns a new instance of the base type object
in Python. It can be used as a placeholder for an object of any type, to create an instance of an object, and to check the type of an object. Understanding how to use the object()
function is an important part of mastering Python programming.