Skip to content

Instantly share code, notes, and snippets.

@jlettvin
Last active May 13, 2016 18:22
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 jlettvin/1344c1511971a13fb1f3d927323045f3 to your computer and use it in GitHub Desktop.
Save jlettvin/1344c1511971a13fb1f3d927323045f3 to your computer and use it in GitHub Desktop.
class Dict(dict):
def __init__(self, **kw)
self.__dict__ = self
self.update(kw)
hard = {'hello': 'world'}
easy = Dict(**hard)
assert easy.hello == hard["hello"], "Should not fail" # Because this is the purpose of the class
assert easy["hello"] == hard["hello"], "Should not fail" # Because it should not invalidate old code.
assert easy.hello == hard.hello , "Should fail" # Because a dict doesn't have members
@jlettvin
Copy link
Author

jlettvin commented May 13, 2016

The Dict class enables use of instance.member syntax rather than dictionary[key] syntax.
It behaves as a dictionary for which the keys are also members.
This is similar to the native ecmascript (javascript) syntax conflation.

  • This gist code shows the basic idea.
  • See implementation in code_quality Dict.py.
  • See test_Dict.py for use cases in unit tests.
  • Run "make clean; make" for quality metrics.

Most useful operations on a dictionary are supported.
Deriving from Dict works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment