⬅ Previous Topic
Python next() Function – Retrieve Next Item from an IteratorNext Topic ⮕
Python oct() Function – Convert Number to Octal⬅ Previous Topic
Python next() Function – Retrieve Next Item from an IteratorNext Topic ⮕
Python oct() Function – Convert Number to Octalobject()
FunctionThe object()
function is the most basic built-in function in Python. It returns a featureless object that is the base for all new-style classes.
In Python, object
is the top-most class in the class hierarchy. All classes inherit directly or indirectly from this base class.
object()
obj = object()
print(type(obj))
<class 'object'>
object()
?object
Although it's optional in Python 3, you can explicitly inherit from object
to define a class:
class MyClass(object):
pass
instance = MyClass()
print(isinstance(instance, object))
True
Explicitly using object
helps ensure consistent method resolution order (MRO), especially in multiple inheritance scenarios.
object()
Although featureless, object()
still has some basic methods:
__str__()
__repr__()
__eq__()
__hash__()
object()
– You can't add attributes to it directly.object()
with instantiating a usable structure – It’s only meant as a base type.In interviews, understanding that all Python classes inherit from object
is important. It affects how methods like __init__
and __str__
behave in inheritance.
object()
creates a minimal base object.Create a custom class that inherits from object
and adds a method to greet.
class Greeter(object):
def greet(self):
return "Hello from object!"
obj = Greeter()
print(obj.greet())
Hello from object!
This shows how even a basic class built from object
can be extended for real use.
⬅ Previous Topic
Python next() Function – Retrieve Next Item from an IteratorNext Topic ⮕
Python oct() Function – Convert Number to OctalYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.