To work with Python objects, we must know the attributes of objects because every object has some attributes associated with it. Python attributes are properties of classes and instances. For example, a class named “employee” can have a name, ID and department, etc., as its attributes.
To print object attributes, different methods are used in Python. This Python post uses the following methods to print an object’s attributes.
- Method 1: Using dir() Function to Print Object Attributes
- Method 2: Using vars() Function to Print Object Attributes
- Method 4: Using inspect Module to Print Object Attributes
Method 1: Using dir() Function to Print Object Attributes
In the code below the “dir()” function is used to print the object attributes:
Code:
value = tuple()
print(dir(value))
In the above code, the “dir()” function accepts an object “value” as an argument and returns the valid attribute of an object in the form of a list.
Output:
The above output shows the object attributes of a specific object.
Method 2: Using vars() Function to Print Object Attributes
In the code given below, the “vars()” function is used to print the object attributes in Python:
Code:
from pprint import pprint
class myclass:
variable_name = 1
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
#creating object for defined class
myobje = myclass("Lily", 21, 5.6)
#Without passing any parameter value to vars() function
pprint(vars())
#passing object parameter value to vars() function
pprint(vars(myobje))
#Using the __dict__ attribute
print(myobje.__dict__)
In the above code:
- A class called “myclass” is defined using the keyword “class”.
- A variable and a parameterized constructor are initialized inside the class.
- The object named “myobje” is created, and using this object, different values are assigned to the constructor’s parameters.
- The “vars()” function is passed inside the print() function to return all the attribute instances along with the value in the form of a dictionary.
- When the object “myobje” is passed inside the “vars()” function, it will return the attributes associated with the instance as a dictionary.
Note: An error will appear in a program when the “vars()” function is applied to the object which does not have a “__dir__” attribute.
Output:
The above output shows the object attribute values.
Method 3: Using inspect Module to Print Object Attributes
The “inspect.getmembers()” function prints all the attributes of an object. Let’s understand it via the following code:
Code:
import inspect
class myclass:
variable_name = 1
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
myobject = myclass("Lily", 21, 5.6)
print(inspect.getmembers(myobject))
In the above code:
- The “inspect” module is imported in the program.
- The class name “myclass” with a variable “variable_name” and a constructor “__init__” is defined in the program.
- The “inspect.getmembers()” accepts the object value “myobject” as an argument and returns the attributes of the object.
Output:
The above output shows the attributes of the class object.
Conclusion
To print an object’s attributes, different methods are used in Python, such as the “dir()” function, “vars()” function, and “inspect.getmembers()” function. The “dir()” function accepts an object as an argument and returns all the attributes, including variables, methods, and objects. Similarly, the “vars()” and “getmembers()” functions are used to print the object attributes in Python. This post delivered a tutorial on printing object attributes in Python using various examples.