On Tue, 1 Oct 2019 at 11:24, Steven D'Aprano email@example.com wrote:
On Mon, Sep 30, 2019 at 06:00:44PM +0900, Stephen J. Turnbull wrote:
Technical questions: does "Subscriptable" mean non-negative ints only, or does it include the negative "count from the end" protocol? How about slices?
It means the class defines a __getitem__ method. Like __call__, the semantics of that method, and the range of acceptable arguments, is out of scope of the ABC.
Then what use is this particular ABC? I assume you had a reason for wanting this when starting this thread but I still don't see what that is.
The distinction between a Mapping and a Sequence is important because basic usage is different e.g.:
for x in obj: print(obj[x]) # fine for Mapping but not for Sequence
for n in range(len(obj)): print(obj[n]) # fine for Sequence but not for Mapping
I can see situations where you might want to differentiate between these two in order to do something useful. I would still prefer not to write code that way myself and instead to simply document my expectation that e.g. a mapping is expected. I can see though why someone would want to use isinstance(obj, Mapping) in some situations.
With Subscriptable I don't see a situation where the isinstance test is actually useful. I'm assuming here that your reason for wanting the Subscriptable ABC is to branch on isinstance but maybe that's not right. This discussion would be easier if you would give a clear example where this might be useful.