Skip to content

Instantly share code, notes, and snippets.

@baojie
Last active January 4, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baojie/8627173 to your computer and use it in GitHub Desktop.
Save baojie/8627173 to your computer and use it in GitHub Desktop.
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