On 13 November 2016 at 21:25, Ivan Levkivskyi firstname.lastname@example.org wrote:
This reminds me @ vs .dot() for matrix multiplication a bit. Pandas has IndexSlicer, NumPy has index_exp, etc. I think it would be nice to have a simple common way to express this. But here we have an additional ingredient -- generic types. I think that a reasonable compromise would be to simply continue the way proposed in http://bugs.python.org/issue24379 -- just add operator.subscript for this purpose. Pros:
- subscript is not a class, so that subscript[...] will be not confused with
- this does not require patching built-ins;
- all libraries that need this will get a "common interface" in stdlib,
operator module seems to be good place for this.
From an educational point of view, it also makes it a bit easier to
give interactive examples of how slicing syntax maps to the subscript parameter on __getitem__, __setitem__ and __delitem__: you can just do "print(operator.subscript[EXPR])" rather than having to build a dummy __getitem__ implementation of your own.
If an actual use case is found for it, that approach would also leave "operator.subscript('EXPR')" available for a micro-eval implementation that evaluated a given string as a subscript rather than as a normal top level expression.