You can use post_init for this
from dataclasses import dataclass
@dataclass
class One:
f_one: int
f_two: str
@dataclass
class Two:
f_three: str
f_four: One
def __post_init__(self):
self.f_four = One(**self.f_four)
data = {'f_three': 'three', 'f_four': {'f_one': 1, 'f_two': 'two'}}
print(Two(**data))
# Two(f_three="three", f_four=One(f_one=1, f_two='two'))
Related Contents:
- Calling a function of a module by using its name (a string)
- How to make a class JSON serializable
- How do I determine the size of an object in Python?
- How to build a basic iterator?
- Why do Python classes inherit object?
- Saving an Object (Data persistence)
- Spawning multiple instances of the same object concurrently in python
- Extract first item of each sublist
- Making object JSON serializable with regular encoder
- Saving and loading multiple objects in pickle file?
- Python: How to match nested parentheses with regex?
- How to JSON serialize sets?
- Finding max value in the second column of a nested list?
- How to access outer class from an inner class?
- Best way to save a trained model in PyTorch? [closed]
- Serializing class instance to JSON
- How do you create nested dict in Python?
- What is the difference between __init__ and __call__?
- In Python, how can you load YAML mappings as OrderedDicts?
- How I can I lazily read multiple JSON values from a file/stream in Python?
- Elegant way to check if a nested key exists in a dict?
- Serialize Python dictionary to XML [closed]
- Validating detailed types in python dataclasses
- Is there a recursive version of the dict.get() built-in?
- Using self.xxxx as a default parameter – Python
- Flattening a list recursively [duplicate]
- super() fails with error: TypeError “argument 1 must be type, not classobj” when parent does not inherit from object
- Class inheritance in Python 3.7 dataclasses
- How can I pickle a dynamically created nested class in python?
- Search for a key in a nested Python dictionary
- How to sort OrderedDict of OrderedDict?
- How does one ignore extra arguments passed to a dataclass?
- Django Rest Framework writable nested serializers
- Why is `object` an instance of `type` and `type` an instance of `object`?
- What is the purpose of subclassing the class “object” in Python?
- How to clone a Python generator object?
- How can I use list comprehensions to process a nested list?
- Why do ints require three times as much memory in Python?
- Best method of saving data
- Why does updating one dictionary object affect other? [duplicate]
- How to apply __str__ function when printing a list of objects in Python [duplicate]
- Creating a tree/deeply nested dict from an indented text file in python
- Serializing binary data in Python
- Saving and loading objects and using pickle
- Getting container/parent object from within python
- What does object’s __init__() method do in python? [duplicate]
- How to parse a string and return a nested array?
- How to change the serialization method used by the multiprocessing module?
- Why does object.__new__ work differently in these three cases
- How to use “get_or_create()” in Django?