🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Creating a dictionary from an object's properties is a common task in Python, which can be especially useful in areas like object serialization, where object attributes need to be converted into a format that can be easily stored or transmitted. Python's built-in vars() function can be used to perform this operation.
Definition
A dictionary in Python is a collection of key-value pairs. Creating a dictionary from an object involves extracting the object’s attributes (keys) and their corresponding values.
2. Program Steps
1. Define a class with some attributes.
2. Create an instance of the class.
3. Use the vars() function to convert the object's attributes to a dictionary.
4. Print the resulting dictionary.
3. Code Program
# Define a class with some attributes
class MyClass:
def __init__(self, name, age, country):
self.name = name
self.age = age
self.country = country
# Create an instance of the class
obj = MyClass(name="John", age=30, country="USA")
# Convert the object's attributes to a dictionary
obj_dict = vars(obj)
# Print the dictionary
print("The dictionary created from the object is:")
for key, value in obj_dict.items():
print(f"{key}: {value}")
Output:
The dictionary created from the object is: name: John age: 30 country: USA
Explanation:
1. MyClass is a class with an __init__ method that initializes name, age, and country attributes.
2. obj is an instance of MyClass with attribute values "John", 30, and "USA".
3. vars(obj) is used to convert obj's attributes to a dictionary, where keys are the names of the attributes and values are the attribute values.
4. obj_dict stores the dictionary representation of obj.
5. A for loop iterates over obj_dict.items(), which contains the dictionary items, and prints each key-value pair.
6. The output demonstrates that the attributes of the object have been successfully converted into a dictionary format.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment