How To Get All Attributes Of An Object In Python

The attributes are all characteristics of an Object. Developers can easily get all attributes of an object in Python, using 1 of several built-in functions. In this article, we will go together to learn all the tips to do this work!

How to get all attributes of an object in Python

Here are the top ways to get all attributes of an object in Python.

Using dir() function

The easiest way is to use the Python built-in dir() function. It returns a list that includes all methods and attributes existing in the object.

The syntax for printing all attributes of an object using dir() is like so:

print(dir([object]))

Here is the code sample:

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
print(dir(user))

The output will be:

Using vars() function

vars() is another built-in function in Python that helps you get all object attributes. Unlike dir(), vars() returns a dictionary of all the instance attributes and their values.

The syntax for using vars() is like this:

print(vars([object]))

See the code sample below:

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
print(vars(user))

The output will be:

Using getmembers() method from the module inspect

Another good way to get all attributes of an object in Python is to use the getmembers() method. This method returns a list containing the attributes and methods of an object.

First, you need to import the module inspect on one of the top lines in your Python program:

import inspect

The syntax is like this:

print(inspect.getmembers([object]))

As you use this method, you will receive a list of all attributes and methods in your object, like in the code sample below:

import inspect

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
print(inspect.getmembers(user))

The output is like this:

If you just need to get the attributes, you can customize the code so that it does not print out unnecessary methods.

Below is the code sample:

import inspect

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
for member in inspect.getmembers(user):
    if not member[0].startswith('_'):
        if not inspect.ismethod(member[1]):
            print(member)

The output will look better:

Using __dict__ method

The __dict__ method also returns all attributes and their values of an object in Python. To print the result out, you use the method with the following syntax:

print([object].__dict__)

Here is the sample code:

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
print(user.__dict__)

The result is:

The great thing is, __dict__ supports returning only the attributes without the values. The syntax is like this:

print(user.__dict__.keys())

Here is the sample:

# Create a class User
class User:
    def __init__(self, nameUser, ageUser, sexUser):
        self.nameUser = nameUser
        self.ageUser = ageUser
        self.sexUser = sexUser

# Create an object user
user = User("Andy", 23, "M")

# Get all attributes of the object user
print(user.__dict__.keys())

The result will be:

Summary

To summarize, we have shared with you 4 ways to get all attributes of an object in Python. Among all, the __dict__ method will give you convenience, as you are able to print both attributes and values or just the attributes out. The others are also good to refer to! You are free to decide the best method for your program!

Leave a Reply

Your email address will not be published. Required fields are marked *