Skip to content

Instantly share code, notes, and snippets.

@ewalstad
Last active October 8, 2022 15:42
Show Gist options
  • Save ewalstad/2258153 to your computer and use it in GitHub Desktop.
Save ewalstad/2258153 to your computer and use it in GitHub Desktop.
A Python class-like dictionary, or dictionary-like class, that allows access to values using dot notation. See the docstring for example usage.
class Classtionary(dict):
"""A Class, er, dictionary, um, attributes-as-keys thingy.
>>> c = Classtionary(foo="bar")
>>> d = dict(foo="bar")
>>> c, d
({'foo': 'bar'}, {'foo': 'bar'})
>>> c.fizz = "bang"
>>> d.fizz = "bang"
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'fizz'
>>> d['fizz'] = "bang"
>>> c['fizz']
'bang'
>>> c.fizz
'bang'
>>> d['fizz']
'bang'
>>> d.fizz
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'fizz'
"""
def __init__(self, **kwargs):
self.__dict__ = self
self.update(kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment