Last active
October 8, 2022 15:42
-
-
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.
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
| 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