- 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
How to create a Python dictionary from an object's fields?
The __dict__ attribute returns a dictionary out of fields of any object.
Let us define a class person
>>> class person: def __init__(self): self.name='foo' self.age = 20 def show(self): print (self.name, self.age)
We now declare an object of this class and obtain its __dict__ attribute which turns out to be dictionary object
>>> p = person() >>> d = p.__dict__ >>> d {'name': 'foo', 'age': 20}
- Related Articles
- How to create a Python dictionary from text file?
- How to create a Pandas series from a python dictionary?
- Python Program to Form a Dictionary from an Object of a Class
- How to create Python dictionary from the value of another dictionary?
- How to create Python dictionary from JSON input?
- How to create an empty dictionary in Python?
- Python program to create a dictionary from a string
- In Python how to create dictionary from two lists?
- How to create a dictionary in Python?
- How to manipulate JavaScript's Date object?
- Python – Create dictionary from the list
- Python program to remove leading 0's from an IP address
- How to create nested Python dictionary?
- Binding an object's method to a click handler in JavaScript
- How to create Python dictionary from list of keys and values?

Advertisements