Last active
May 13, 2016 18:22
-
-
Save jlettvin/1344c1511971a13fb1f3d927323045f3 to your computer and use it in GitHub Desktop.
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Most useful operations on a dictionary are supported.
Deriving from Dict works as expected.