Last active
September 24, 2025 00:26
-
-
Save baojie/8627173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from attrdict import AttrDict | |
| a = AttrDict({'foo': 'bar'}) | |
| a.foo | |
| # OUT: 'bar' | |
| a.foo == a['foo'] | |
| # OUT: True | |
| a.foo = 'yeah' | |
| a['foo'] | |
| # OUT: 'yeah' | |
| foo in a | |
| # OUT: Traceback (most recent call last): | |
| # OUT: File "<input>", line 1, in <module> | |
| # OUT: NameError: name 'foo' is not defined | |
| 'foo' in a | |
| # OUT: True | |
| a.keys() | |
| # OUT: ['foo'] | |
| del a.foo | |
| a | |
| # OUT: a{} | |
| a.get('foo', 0) | |
| # OUT: 0 | |
| a.foo = 'bar' | |
| a.get('foo', 0) | |
| # OUT: 'bar' | |
| a = AttrDict({'foo': {'bar': 'baz'}}) | |
| a.foo.bar | |
| # OUT: 'baz' | |
| a.foo['bar'] | |
| # OUT: 'baz' | |
| a['foo']['bar'] | |
| # OUT: 'baz' | |
| a['foo'].bar | |
| # OUT: 'baz' | |
| a('foo') | |
| # OUT: aa{'bar': 'baz'} | |
| a('foo').bar | |
| # OUT: 'baz' | |
| a = AttrDict({'list': [{'value': 1}, {'value': 2}]}) | |
| for x in a.list: print x | |
| # OUT: aa{'value': 1} | |
| # OUT: aa{'value': 2} | |
| for x in a.list: print x.value | |
| # OUT: 1 | |
| # OUT: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment