Here, let us see how to get a dictionary from a field in a Python object. We must retrieve the class members when we want to get a dictionary from an object’s field. To get the object attributes in dictionary format, we can use one of the two ways given below:
Getting a Dictionary from an Object’s Fields in Python
Below are the methods to get a dictionary from an object’s fields:
Method #1: Using __dict__ attribute
Approach:
- Create a class say, student
- Create a constructor function that initializes the default values of all the variables
- Taking some random class/Instance variables and initializing them with some random values
- Creating a method/function to print all the variables values of the class object creating an object of the above class
- Calling the display() method of the student class using the above-created object
- Calling the __dict__ attribute on the above-created object and printing it.
- The Exit of the Program.
Below is the implementation:
# Create a class say student class student: # Create a constructor function which initializes the default values of all the variables def __init__(self): #Taking some random class/Instance variables and intializing them with some random values self.rollno = 1 self.marks = 85 # Creating a method/function to print all the variables values of the class object def display(self): print('The values of roll number and marks are :',self.rollno, self.marks) # Creating object of the above class result = student() # Calling the display() method of the student class using the above created object result.display() # Calling the __dict__ attribute on the above created object and printing it print(result.__dict__)
Output:
The values of roll number and marks are : 1 85 {'rollno': 1, 'marks': 85}
Explanation:
Using the __dict__ attribute, we can create a dictionary from any object. In Python, every object has a __dict__ attribute. When we call this attribute, we get a dictionary-formatted output object. The resulting dictionary contains all of the attributes of the object under examination. The dictionary is created by mapping attributes to their values.
Within the constructor method, we initialized the instance variables as keys with their respective values. Then, to print the values, we defined another method. Finally, we created an object of the declared class and used its __dict__ attribute to retrieve the dictionary.
Method #2: Using vars built-in method
Approach:
- Create a class say, student
- Create a constructor function that initializes the default values of all the variables
- Taking some random class/Instance variables and initializing them with some random values
- Creating a method/function to print all the variables values of the class object creating an object of the above class
- Calling the display() method of the student class using the above-created object
- Pass the above object to the vars() method and print it
- The Exit of the Program.
Below is the implementation:
# Create a class say student class student: # Create a constructor function which initializes the default values of all the variables def __init__(self): # Taking some random class/Instance variables and intializing them with some random values self.rollno = 1 self.marks = 85 # Creating a method/function to print all the variables values of the class object def display(self): print('The values of roll number and marks are :',self.rollno, self.marks) # Creating object of the above class result = student() # Calling the display() method of the student class using the above created object result.display() # Pass the above object to the vars() method and print it print(vars(result))
Output:
The values of roll number and marks are : 1 85 {'rollno': 1, 'marks': 85}
Explanation:
We can also use the built-in vars() method to accomplish the same thing. The vars() method returns a __dict__ attribute of a module, class, or object.
In this case, we used the vars() method instead of the __dict__ attribute on the result object of the student class
Both of the preceding methods will return the object attributes in dictionary format. In this case, we printed the attributes of a newly defined object. This attribute, however, can be applied to any object whose contents can be inspected in dictionary format.