Print Object Attributes in Python: 6 Methods

You already know that there are attributes in a class that are defined by the developers. When you create an object of the class then all its attributes are also copied in the created object. Lets say you want to print the object attributes in python, then how you will do. In this tutorial you will know how to print Object Attributes in Python using the various methods.

Methods to Print Object Attributes in Python

Method 1: Using the print() function with dot notation

The simplest way to print object attributes is by using the print() function with the dot notation. Let’s assume You have a class named Person with attributes such as name, age, and location.

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

person = Person("John Doe", 30, "New York")
print(person.name)
print(person.age)
print(person.location)

Output

John Doe
30
New York

In this method, I created an instance of the Person class named person and initialize its attributes. I then use the print() function with dot notation to print the values of each attribute.

Method 2: Using the getattr() function

The getattr() function in Python allows you to access object attributes dynamically. This means you can specify the attribute name as a string and retrieve its value. Let’s see an example of how to use getattr() to print object attributes.

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

person = Person("John Doe", 30, "New York")
print(getattr(person, 'name'))
print(getattr(person, 'age'))
print(getattr(person, 'location'))
John Doe
30
New York

In this example, you created an object of the Person class named person and initialize its attributes. You will then use the getattr() function to retrieve the values of each attribute dynamically.

Method 3: Using the vars() function

The vars() function in Python returns the dict attribute of an object, which is a dictionary containing the object’s attributes and their values. You can use the vars() function to print all attributes of an object.

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

person = Person("John Doe", 30, "New York")
attributes = vars(person)
for attribute, value in attributes.items():
    print(attribute, "=", value)

Output

name = John Doe
age = 30
location = New York

In the above code, I have created an instance of the Person class named person and initialize its attributes. Then I use the vars() function to retrieve a dictionary of all attributes and their values. I iterated over the dictionary using a for loop and print each attribute and its value.

Method 4: Using the dir() function

The dir() function in Python returns a list of all attributes and methods of an object. You can use this function to print the attributes of an object. Below is the example of it.

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

person = Person("John Doe", 30, "New York")
attributes = dir(person)
for attribute in attributes:
    if not attribute.startswith("__"):
        print(attribute, "=", getattr(person, attribute))

Output

name = John Doe
age = 30
location = New York

In this example, First I have created an instance of the Person class named person and initialize its attributes. Then I use the dir() function to get a list of all attributes and methods of the person object. Lastly iterate over the list and print only the attributes (excluding the ones that start with double underscores).

Method 5: Using a dictionary

If You know the attributes of an object in advance, then you can define a dictionary that maps attribute names to their corresponding values. After that you can then print the attributes using the dictionary. Here’s an example

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

person = Person("John Doe", 30, "New York")
attributes = {
    "name": person.name,
    "age": person.age,
    "location": person.location
}
for attribute, value in attributes.items():
    print(attribute, "=", value)

Output

name = John Doe
age = 30
location = New York

Here, First I create an instance of the Person class named person and initialize its attributes. The I have defined a dictionary attributes that maps each attribute name to its corresponding value. Then using the for loop I have iterated over the dictionary and print each attribute and its value.

Method 6: Using the inspect module

The inspect module in Python provides several functions to get information about live objects. You can use the inspect module to print object attributes. Below is the example of this method.

import inspect

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

person = Person("John Doe", 30, "New York")
attributes = inspect.getmembers(person, lambda a: not(inspect.isroutine(a)))
for attribute in attributes:
    print(attribute[0], "=", attribute[1])

Here I have imported the inspect module. Then created an instance of the Person class named person and initialize its attributes. After that used the inspect.getmembers() function to get a list of attributes of the person object. Lastly using the for loop iterated over the list and print each attribute and its value.

Conclusion

Class is widely used oops concept among developers. It have attributes that are automatically called when you create an object or instance of the classe. The above are the methods to Print Object Attributes in Python.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link