- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- How to create a Python dictionary from an object's fields?
- Python program to create a dictionary from a string
- Program to find number of ways to form a target string given a dictionary in Python
- Swift Program to Print object of a class
- How to extract subset of key-value pairs from Python dictionary object?
- How to remove a key from a python dictionary?
- Golang Program to Create a Class and Object
- Swift program to create a class and object
- Python program to convert a list of tuples into Dictionary
- Python - Ways to remove a key from dictionary
- How to convert a string to a Python class object?
- What is the best way to remove an item from a Python dictionary?
- Python Program – Create dictionary from the list
- C# program to get the List of keys from a Dictionary
- How to create a Pandas series from a python dictionary?

Advertisements