- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Form a Dictionary from an Object of a Class
When it is required to form a dictionary with the help of an object and class, a class is defined. An ‘init’ function is defined, that assigns values to variables. An instance of the class is created, and the init function is called.
Example
Below is a demonstration for the same −
class base_class(object): def __init__(self): self.A = 32 self.B = 60 my_instance = base_class() print("An instance of the class has been created") print(my_instance.__dict__)
Output
An instance of the class has been created {'A': 32, 'B': 60}
Explanation
- A ‘base_class’ is defined, and an object is passed to it.
- An ‘init’ method is defined, and it assigns values to variables.
- An instance of the method is created.
- The method is called using the obect created, using ‘.’ operator
Advertisements
To Continue Learning Please Login
Login with Google