Python object() Method

The object() is a built-in method of python that returns an empty object that is the base for all the classes.

Python object()

The syntax of python object() method is:


Python object() Method

The object() method doesn’t take any parameter.

Let see an example of an object() method in python.

Example 1: How to use object() method?

test = object()




<class 'object'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

Example 2: How python get size of object?

In objects are store in pythin so to get the sixe of python object we are going to ose sys.getsizeof() function from the sys python moduel.

import sys

# Create an object
my_list = [1, 2, 3, 4, 5]

# Get the size of the object
size = sys.getsizeof(my_list)

print(f"Size of my_list in bytes: {size} bytes")


Size of my_list in bytes: 104 bytes

Example 3: python convert object to string

To convert an object to a string in python we can use python str() method.This function attempts to return a string representation of the object.

my_object = 42
my_string = str(my_object)

The output will be as follow:


Example 4: list attributes of object python

# Create an example object
class MyClass:
    def __init__(self, value):
        self.value = value

    def say_hello(self):

my_object = MyClass(42)

# List attributes of the object
attributes = dir(my_object)



['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'say_hello', 'value']

Here we have python print object attributes using object() method.

Rules of the object()

  • The object() method will only return an empty object.

Other References

  • Python getattr() – getattr() method is used to access the values of attributes of python objects.
  • Python id() – id() method in python returns the unique identity number of an object.
  • Python enumerate() – enumerate() method adds a counter to an iterable and returns its enumerated object.