close

How to print Object’s Attributes in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to print Object’s Attributes in Python. so without wasting time lets learn about of this.

How to print Object’s Attributes in Python

  1. print Object's Attributes in Python

    To print Object's Attributes in Python Use  vars() mixed with pprint(): from pprint import pprint pprint(vars(perticular_your_object)) This method will help you in printing the attribute in python.

  2. How to print Object's Attributes in Python

    Use dir()vars() or the inspect module. This will help you in this.You will understand it better by below example. l = dir(__builtins__) d = __builtins__.__dict__
    Print that dictionary however fancy you like: print l ['ArithmeticError', 'AssertionError', 'AttributeError',...

Method 1: Use vars()

Use  vars() mixed with pprint():

from pprint import pprint
pprint(vars(perticular_your_object))

This method will help you in printing the attribute in python.

Method 2: Use dir()

Use dir()vars() or the inspect module. This will help you in this.

You will understand it better by below example.

l = dir(__builtins__)
d = __builtins__.__dict__

Print that dictionary however fancy you like:

print l
['ArithmeticError', 'AssertionError', 'AttributeError',...

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment