Skip to content

Python attrdict trap

AttrDict is an MIT-licensed library that provides mapping objects that allow their elements to be accessed both as keys and as attributes.

It's already not support the current python version now, for the lower version python, it's a useful library.

However, there is some magic behavior while using it.

For example,

Python
data = AttrDict({'names': ['dog', 'cat']})

print(type(data.names)) # what's the output? list type?
The output is
Text Only
<class 'tuple'>

So, if you want to add new items into the original list by data.names.append('birth'), it will raise AttributeError: 'tuple' object has no attribute 'append'.