In python, how do I cast a class object to a dict
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

