On 2/09/20 5:14 am, Stephan Hoyer wrote:
On Tue, Sep 1, 2020 at 9:59 AM David Mertz <mertz@gnosis.cx I think we need this for the same reason why we need **kwargs in normal function calls: it makes it possible to forward dictionaries of arguments to another method.
If we don't have dict unpacking, you'd have to write obj.__getitem__(**kwargs) rather than obj[**kwargs], which is much more verbose and goes against the principle that you shouldn't need to call double-underscore methods in normal Python code.
It's also not even quite correct! As another message posted recently pointed out, if 'other' happens to be a class object, __class_getitem__ should be called instead, if it exists:
def __getitem__(self, index, **kwds): g = None if isinstance(other, type): g = getattr(other, '__class_getitem__', None) if g is None: g = other.__getitem__ return g(index, **kwds)
This is getting pretty tricky and convoluted. (I'm not sure it's completely correct even now, since it doesn't check whether __class_getitem__ is a class method.) It certainly won't remain correct if the rules for indexing method lookup change in the future.
If a[**kwds] is allowed, on the other hand, it's easy to write index forwarding code that is straightforward, obvious and future-proof.