{"id":1308,"library":"addict","title":"Addict","description":"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.","status":"active","version":"2.4.0","language":"en","source_language":"en","source_url":"https://github.com/mewwts/addict","tags":["dictionary","attribute access","dot notation","nested dictionaries"],"install":[{"cmd":"pip install addict","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Dict","correct":"from addict import Dict"}],"quickstart":{"code":"from addict import Dict\n\n# Create an Addict dictionary\nbody = Dict()\n\n# Set nested values using dot notation\nbody.query.filtered.query.match.description = 'addictive'\nbody.query.filtered.filter.term.created_by = 'Mats'\n\nprint(body)\n# Access values\nprint(body.query.filtered.query.match.description)\n\n# Standard dict operations also work\nbody['metadata'] = {'source': 'API'}\nprint(body.metadata.source)\n","lang":"python","description":"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."},"warnings":[{"fix":"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 = {}`.","message":"In version 2.0.0, accessing a non-existent key via attribute (e.g., `my_dict.non_existent_key`) no longer automatically creates an empty `Dict` and adds it to the parent. It now returns an empty `Dict` but *does not* modify the original `my_dict`. To add a key, you must assign a value (e.g., `my_dict.new_key = {}`).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"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.","message":"Keys that are not valid Python identifiers (e.g., integers, keys with spaces or special characters) cannot be accessed or set using attribute (dot) syntax. These must still be accessed using standard item (square bracket) syntax.","severity":"gotcha","affected_versions":"all"},{"fix":"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.","message":"When initializing a `Dict` with an iterable containing other container types (like a list of dictionaries), `addict` clones these values and converts nested dictionaries into `Dict` instances. However, if you later assign an existing container type via attribute or item syntax, `addict` generally maintains the reference to the original object, leading to different behavior than during initial construction.","severity":"gotcha","affected_versions":"all"},{"fix":"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.","message":"The `freeze()` method (introduced in v2.4.0) prevents the creation or retrieval of missing keys, raising a `KeyError` or `AttributeError` instead. This changes the default 'autovivification' behavior of `addict` and can lead to unexpected errors if not handled.","severity":"gotcha","affected_versions":">=2.4.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}