On Tue, Aug 5, 2014 at 10:31 PM, Skip Montanaro <s...@pobox.com> wrote: > JavaScript objects have that feature. I find it mildly confusing > because whenever I see it I have to pause to consider whether the name > I am looking at is an attribute or a key. This little JS code I just > typed at my console prompt was also mildly surprising: > >> var x = {}; > undefined >> x.valueOf(47) > Object {} >> x["valueOf"] = 12 > 12 >> x.valueOf > 12 >> x.valueOf(47) > TypeError: number is not a function
This is partly a consequence of prototype-based inheritance; x.valueOf will shadow Object.valueOf. Pike allows a similar "dot or square brackets" notation, but at the expense of not having any methods on the mapping type itself: Pike v8.0 release 3 running Hilfe v3.5 (Incremental Pike Frontend) > mapping a=([]); > a.foo="bar"; (1) Result: "bar" > a.foo; (2) Result: "bar" > a; (3) Result: ([ /* 1 element */ "foo": "bar" ]) Since mappings (broadly equivalent to Python dicts) can't have methods, anything that Python does as a method, Pike has to do as a stand-alone function. (Swings and roundabouts, though, as those functions tend to also accept other types, so they're more akin to Python's len() than dict.pop().) Every design decision has a cost. The convenience of short-handing mapping lookup is pretty handy (especially when you're digging into deeply-nested mappings - imagine parsing an XML or JSON message and then reaching into it for one specific thing), but it means there are functions rather than methods for working with them. Take your pick. ChrisA -- https://mail.python.org/mailman/listinfo/python-list