[Python-Dev] Re: Improvement to SimpleNamespace

2020年4月28日 11:26:23 -0700

On 2020年04月16日 04:33, Rob Cliffe via Python-Dev wrote:
Here's another revolutionary thought: add a new operator and associated dunder method (to object?) whose meaning is *undefined*. Its default implementation would do *nothing* useful (raise an error? return None?).
E.g. suppose the operator were `..`
Then in a specific class you could implement x..y to mean x['y']
and then you could write
   obj..abc..def..ghi
Still fairly concise, but warns that what is happening is not normal attribute lookup.
Interesting, I've thought the same thing. Double dot might be a good option.
In practice however I've not encountered key names in JSON that conflict with the dictionary methods. A missing protocol could handle clashes when they happen, as applied to keys. Keys that conflict are simply shadowed by the method names unless you use [''] notation. I know, that answer is not satisfying to the purist. Double dot is better in that regard. Yet haven't found it to be a concrete problem. Perhaps linters could find code using uncalled dict method names as a mitigation. Suppose it boils down to a judgement call in the end.
-Mike
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/YPMKEB3ASW7KJAIP6F7K3F3X7FHIINRN/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to