w3resource

Python: Get a dictionary from an object's fields

Python dictionary: Exercise-16 with Solution

Write a Python program to get a dictionary from an object's fields.

Sample Solution:-

Python Code:

# Define a class 'dictObj' that inherits from the 'object' class.
class dictObj(object):
    # Define the constructor method '__init__' for initializing object attributes.
    def __init__(self):
        # Initialize attributes 'x', 'y', and 'z' with string values.
        self.x = 'red'
        self.y = 'Yellow'
        self.z = 'Green'
    
    # Define a method 'do_nothing' that doesn't perform any actions (placeholder).
    def do_nothing(self):
        pass

# Create an instance 'test' of the 'dictObj' class.
test = dictObj()

# Print the '__dict__' attribute of the 'test' object, which contains its attribute-value pairs.
print(test.__dict__) 

Sample Output:

{'x': 'red', 'y': 'Yellow', 'z': 'Green'}

Python Code Editor:

Previous: Write a Python program to get the maximum and minimum value in a dictionary.
Next: Write a Python program to remove duplicates from Dictionary.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.