attmap
raw JSON → 0.14.0 verified Mon Apr 27 auth: no python
Multiple access patterns for key-value reference. attmap provides AttMap, a mapping subclass that allows attribute-style access (e.g., obj.key) alongside dict-style access (e.g., obj['key'] or obj.get('key')). It is useful for configuration objects, simple namespaces, and data structures where you want flexible key access. Version 0.14.0 is stable, requires Python >=3.8, and is maintained on GitHub (pepkit/attmap).
pip install attmap Common errors
error AttributeError: 'AttMap' object has no attribute 'key' ↓
cause The key does not exist or is only accessible via dict-style access because the key string is not a valid identifier.
fix
Check that the key exists and is a valid Python identifier; if not, use obj['key'] instead.
error KeyError: 'key' ↓
cause The key does not exist in the AttMap.
fix
Use .get('key', default) to avoid the error, or ensure the key exists before access.
error TypeError: 'AttMap' object does not support item assignment ↓
cause Trying to assign to a frozen or read-only AttMap instance (if such variant is used).
fix
Check if you are using a read-only variant; use the standard AttMap for mutable operations.
Warnings
gotcha AttMap keys are case-sensitive; attribute access matches dict key exactly (including case). ↓
fix Use identical case for attribute access as the dict key.
gotcha Keys must be valid Python identifiers for attribute access. Keys that are not valid identifiers (e.g., containing spaces or starting with a digit) can only be accessed via dict-style subscript or get(). ↓
fix Avoid keys that are not valid identifiers if you need attribute access, or use obj['key'].
gotcha Inherited attributes from dict (e.g., keys(), items(), etc.) may shadow stored keys. If you store a key with the same name as a dict method, the dict method takes precedence when accessed as an attribute. ↓
fix Access such keys via dict-style subscript: obj['keys'].
deprecated The 'AttMap' class is the recommended name; older aliases like 'AttrDict' may be removed in future versions. ↓
fix Use 'AttMap' instead of any deprecated aliases.
Imports
- AttMap
from attmap import AttMap
Quickstart
from attmap import AttMap
config = AttMap()
config.host = 'localhost'
config['port'] = 8080
print(config.host) # localhost
print(config['port']) # 8080
print(config.get('nonexistent', 'default')) # default