3 ms·
I don't think you need to use metaclasses, you can just define the relevant magic methods, something like: class JSLikeObject(dict): __getattr__ =
by voyou 12y ago
I don't think you need to use metaclasses, you can just define the relevant magic methods, something like:
class JSLikeObject(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
ETA: That gets you JavaScript-style equivalence between attribute and index lookup, but it doesn't get you JavaScript-like binding of "this". I think you could probably do that with descriptors (and maybe it would require writing a metaclass).
- icebraining 12y agoYeap: http://code.activestate.com/recipes/473786-dictionary-with-attribute-style-access/ http://code.activestate.com/recipes/473786-dictionary-with-a...