0

I wonder if there's standard library's support in Python for doing next thing:

x = {'foo': 123, 'bar': 'hello'}
y = std_lib.attr_dict(x)
y.onemore = [1,2,3]
print(y.foo, y['foo'], y.bar, y['bar'], y.onemore, y['onemore'], y.get('notexist', 'NE'))
# prints: 123 123 hello hello [1,2,3] [1,2,3] NE
print(list(dict(y).keys())) # Back conversion to dict
# prints: ['foo', 'bar', 'onemore']

Like there is collections standard library, it might have such attr_dict() class.

I'm pretty sure there are good implementations of this in pip modules, can you suggest the most-popular/most-rich pip modules for doing that?

Or maybe it can be also achieved in just few lines of pure Python code somehow?

Also would be interesting to support nested dictionaries/attributes, like y.first.second = 123.

Arty
  • 14,883
  • 6
  • 36
  • 69

1 Answers1

0

https://stackoverflow.com/a/32107024/10786110 - replace iteritems with items (Python 3)

akiva
  • 369
  • 1
  • 9