Addict is a Python library that provides a dictionary subclass allowing items to be accessed and set using both attribute (dot notation) and item (square bracket) syntax. It simplifies working with deeply nested data structures. The library is currently at version 2.4.0 and demonstrates active maintenance with regular updates.
pip install addictVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a `Dict` instance and populate nested keys using convenient attribute (dot) notation. It also shows accessing values and combining with standard dictionary item assignment.
Explicitly assign an empty `Dict` or any value to the new key to ensure it's added, e.g., `my_dict.new_key = Dict()` or `my_dict.new_key = {}`.Use `my_dict[2]` or `my_dict['key-with-hyphen']` instead of `my_dict.2` or `my_dict.key-with-hyphen` for non-string or non-identifier keys.
Be aware of whether you are initializing with a structure or later assigning references. If deep conversion is always desired, consider using `addict.Dict(your_data)` for initial setup or manually converting nested structures.
Understand when `freeze()` is active. If you need to add or access potentially missing keys on a frozen `Dict`, call `my_dict.unfreeze()` first, or check for key existence before access.
Use bracket notation to access dictionary keys, e.g., `task['id']` instead of `task.id`.
Use the appropriate method for adding items to a dictionary, such as `dict[key] = value`.
Replace `iteritems()` with `items()` to iterate over dictionary items in Python 3.x.
To add a new key-value pair to a dictionary, use `dict[key] = value`.
Ensure the object is a dictionary before calling `update`, or convert it to a dictionary if necessary.
No dependency data recorded yet.