To print an object’s attributes using Python, the easiest way is with the dir() function.

list_object = [1, 2, 3]

print(dir(list_object))

#Output:
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

To get all of the attributes of an object via the class, you can use vars() or __dict__ to print attributes of an object.

import pprint 

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

pprint.pprint(vars(Person))
pprint.pprint(Person.__dict__)

#Output:
{'__dict__': ,
              '__doc__': None,
              '__init__': ,
              '__module__': '__main__',
              '__weakref__': }
{'__dict__': ,
              '__doc__': None,
              '__init__': ,
              '__module__': '__main__',
              '__weakref__': }

When working with objects in Python, it is useful to be able to see all of the attributes of an object.

We can easily print the attributes of objects in Python with the dir() function.

Below is a simple example of getting and printing the attributes of a list object in Python with dir().

list_object = [1, 2, 3]

print(dir(list_object))

#Output:
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Printing Object Class Attributes with vars() Function in Python

If you are working with classes and want to list the attributes of the class, then you can use the vars() function.

The vars() function return the ‘__dict__’ attribute from the class and will list all of the attributes.

For example, let’s say we have a class “Person” as shown below.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

To list all of the attributes of “Person”, we can pass it to vars().

Let’s also use the pprint module here to pretty print the returned dictionary of attributes.

import pprint 

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

pprint.pprint(vars(Person))
pprint.pprint(Person.__dict__)

#Output:
{'__dict__': ,
              '__doc__': None,
              '__init__': ,
              '__module__': '__main__',
              '__weakref__': }
{'__dict__': ,
              '__doc__': None,
              '__init__': ,
              '__module__': '__main__',
              '__weakref__': }

Example of Using vars() to Print List Class Attributes in Python

Here, I’d like to show an example of how to print the class attributes of the list class in Python. Lists are one of the most commonly used objects in Python.

To print all of the class attributes of the class list, we pass ‘list’ to vars().

Below is an example in Python of how to use vars() to print attributes of the class ‘list’.

import pprint 

pprint.pprint(vars(list))

{'__add__': ,
              '__class_getitem__': ,
              '__contains__': ,
              '__delitem__': ,
              '__doc__': 'Built-in mutable sequence.n'
                         'n'
                         'If no argument is given, the constructor creates a '
                         'new empty list.n'
                         'The argument must be an iterable if specified.',
              '__eq__': ,
              '__ge__': ,
              '__getattribute__': ,
              '__getitem__': ,
              '__gt__': ,
              '__hash__': None,
              '__iadd__': ,
              '__imul__': ,
              '__init__': ,
              '__iter__': ,
              '__le__': ,
              '__len__': ,
              '__lt__': ,
              '__mul__': ,
              '__ne__': ,
              '__new__': ,
              '__repr__': ,
              '__reversed__': ,
              '__rmul__': ,
              '__setitem__': ,
              '__sizeof__': ,
              'append': ,
              'clear': ,
              'copy': ,
              'count': ,
              'extend': ,
              'index': ,
              'insert': ,
              'pop': ,
              'remove': ,
              'reverse': ,
              'sort': }

Hopefully this article has been helpful for you to learn how to print object attributes using Python.

Categorized in:

Python,

Last Update: March 14, 2024