Editor

Choose the Right Container

Advanced · Write the code · from NamedTuple vs TypedDict vs Dataclass: Which to Choose?

From the lesson: NamedTuple vs TypedDict vs Dataclass: Which to Choose?

Python gives you three popular ways to bundle related data together: NamedTuple, TypedDict, and dataclass. Each one solves a slightly different problem.

Read the lesson

Write a function called describe_container that takes a string argument. If the argument is 'immutable', return 'NamedTuple'. If it's 'json', return 'TypedDict'. For anything else, return 'dataclass'. Print the result for each of these inputs: 'immutable', 'json', 'flexible'.

# Write your function here
# Test with three inputs

More NamedTuple and TypedDict exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz