In python, how do I cast a class object to a dict
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python, there is no single universal "cast this object to a dict" operation that works correctly for every class. The right approach depends on what kind of object you have: a normal instance, a dataclass, a nested object graph, or something that needs a custom serialized representation.
Use __dict__ or vars() for ordinary instances
For regular Python objects with writable instance attributes, the simplest answer is __dict__:
Output:
vars(user) does the same thing:
This is the most direct solution when you only need the instance attributes exactly as stored on the object.
Use dataclasses.asdict for dataclasses
If the class is a dataclass, use the tool built for it:
This is better than reaching for __dict__ because asdict(...) also handles nested dataclasses recursively.
For example:
That gives you a fully nested dictionary structure.
Add a custom to_dict method when you need control
Many real applications need more than raw attributes. Maybe you want to rename fields, omit private values, or convert dates into strings. In those cases, define the representation explicitly:
This is often the best approach for APIs and serialization because it makes the contract explicit instead of exposing the object's internal storage mechanically.
Know the limits of __dict__
__dict__ is convenient, but it is not a universal serializer.
It has a few important limitations:
- it only includes instance attributes stored on the object
- it does not automatically recurse into nested custom objects
- it may include values that are not JSON-serializable
- it does not work the same way for classes using
__slots__
For example, this object has no ordinary __dict__ storage:
In cases like that, a custom to_dict method is the safer choice.
A reusable helper for plain objects
If you want a lightweight helper for normal attribute-based objects, you can build one:
This filters out private-style attributes and gives you more control than exposing __dict__ directly.
Common Pitfalls
The biggest mistake is assuming __dict__ means "safe to serialize." It only reflects the current instance attribute mapping; it does not guarantee JSON compatibility or a stable external format.
Another common issue is trying to use asdict(...) on a normal class that is not a dataclass. That function is specifically for dataclass instances.
People also forget about nested objects. A shallow dictionary can still contain custom objects that need another conversion step.
Finally, if the class uses __slots__, descriptors, or computed properties, there may not be a useful automatic mapping at all. That is usually a sign to define to_dict explicitly.
Summary
- Use
__dict__orvars()for simple ordinary Python instances. - Use
dataclasses.asdict(...)for dataclass objects, especially when nesting is involved. - Define
to_dict()when you need control over names, formatting, or filtering. - Do not assume a raw attribute mapping is ready for JSON or API output.
- Custom classes with
__slots__or special behavior often need an explicit conversion method.
Related reading
- In Python, how do I index a list with another list?
- In Python, how do I iterate over a dictionary in sorted key order?
- In Python, how do you find the index of the first value greater than a threshold in a sorted list?
- In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order?
- In Python, how do I create a string of n characters in one line of code?
- In Python, how do I indicate I''m overriding a method?
- In Python, when to use a Dictionary, List or Set?
- In Swift, an efficient function that separates an array into 2 arrays based on a predicate

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.