This is a Python Program to form a dictionary from an object of a class.
The program forms a dictionary from an object of a class.
1. Declare a class named A.
2. Initialize the keys with their values in the __init__ method of the class and form a dictionary using the __dict__ method.
3. Print the dictionary formed from the object of the class.
4. Exit.
Here is source code of the Python Program to form a dictionary from an object of a class. The program output is also shown below.
class A(object): def __init__(self): self.A=1 self.B=2 obj=A() print(obj.__dict__)
1. A class named A is declared.
2. The keys are initialized with their values in the __init__ method of the class.
3. The dictionary is formed using the object of the class and by using the __dict__ method.
4. The dictionary formed from the object of the class is printed.
Case 1: {'A': 1, 'B': 2}
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Check Python Books
- Apply for Programming Internship
- Check Information Technology Books
- Apply for Python Internship
- Practice Programming MCQs