3 ms·
I thought this was something implemented using metaclasses to not have to use dictionary-style field access... (e.g., obj.field instead of obj['field'] )
by wbkang 12y ago
I thought this was something implemented using metaclasses to not have to use dictionary-style field access... (e.g., obj.field instead of obj['field'] )
- voyou 12y agoI 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...