On 09/19/2012 06:24 AM, Pierre Tardy wrote: > One thing that is cooler with java-script than in python is that dictionaries > and objects are the same thing. It allows browsing of complex hierarchical > data syntactically easy.
You probably need some different terminology, since a dictionary is already an object. So's an int, or a list, or anything else visible in python. You're trying to blur the distinction between attribute access and access by key (square brackets). > > For manipulating complex jsonable data, one will always prefer writing: > buildrequest.properties.myprop > rather than > brdict['properties']['myprop'] So what you want is to provide a dict-like class which has both a __getitem__ and a __getattribute__, which produces mostly the same results, if the parameters happen to be reasonable and not conflict with other methods. (Similar for *set*, *del*, and __contains__ and maybe others). This has been proposed and discussed and even implemented many times on this list and others. > This ability in JS is well known for its flaws (e.g. > http://drupal.org/node/172169#forin ), and I understand why this is not a > feature that we want in python by default. I did work on class that adds this > feature, and that I wish to use for manipulating my json data. There are many more flaws than just the hiding of certain items because of existing attributes. Additionally, this would only work for items whose keys are strings, and are strings that happen to be legal symbol names and not keywords. If you also support __setitem__ or __delitem__ you run the risk of arbitrary code trashing the code that makes the class work. > The following github pull request to buildbot has tests that defines > specification of such a class, and has several commits, which gives several > implementation of the same thing. > https://github.com/buildbot/buildbot/pull/525 > > All implementation I tried are much slower than a pure native dict access. > Each implementation have bench results in commit comment. All of them are > 20+x slower than plain dict! Assuming you're talking about CPython benchmarks, the dict is highly optimized, C code. And when you provide your own __getitem__ implementation in pure python, there are many attribute accesses, just to make the code work. > I would like to have python guys advices on how one could optimize this. Use C code and slots. > I'd like to eventually post this to python-dev, please tell if this is really > not a good idea. > > Regards, > Pierre if you're proposing a new module for the stdlib, one of the (unstated?) requirements is that it be in regular use by a fairly large audience for a while. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list