On Wed, Aug 26, 2020 at 9:39 PM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote:
> If we use a new set of dunders, the signature of the one for > setting could be > > def __setindex__(self, value, *args, **kwds) > > which is not completely unsurprising, but plays better with > an arbitrary number of positional indexes. > > -- > Greg > On Wed, Aug 26, 2020 at 9:34 PM Greg Ewing <greg.ew...@canterbury.ac.nz> wrote: > On 27/08/20 4:32 am, Ricky Teachey wrote: > > > class Q: > > def __subscript__(self, method_name, *args, **kwargs): > > return getattr(self, method_name)(*args, **kwargs) > > def __getitem__(self, *args, **kwargs): ... > > # Note that I have made the RHS value the first argument in > __setitem__ > > def __setitem__(self, value, *args, **kwargs): ... > > def __delitem__(self, *args, **kwargs): ... > > This seems like a very convoluted way of going about it as > opposed to just adding a new full set of dunders. And I don't > see how it actually provides any more flexibility. > > -- > Greg That version of the idea I gave isn't great, I don't disagree. It wasn't really intended to be the proposed dunder function.... just the quickest way I could think of to write it up to attempt to illustrate to Steve in the bit below: > With the proposal, the language would support any function desired to turn > > the "stuff" inside a subscripting operation into the item dunder calls. > > I'm sorry, I don't understand that sentence. > I think the argument for providing a SINGLE new dunder, rather than three, could be that you don't have two sets of dunders that do the same thing, just with different signatures. The purpose of a new single dunder should simply be to translate the contents of the subscript operator to the existing dunders. The thing I am struggling with is how would it RETURN that translation...? In my slapdash implementation I just CALLED them, but that seems less than ideal. I supposed it could do it by returning a tuple and a dict, like this: def __key_or_index_translator__(self, pos1, *, x) -> Tuple[Tuple[Any], Dict[str, Any]]: """I only allow subscripting like this: >>> obj[1, x=2] """ return (pos1,), dict(x=x) The return value then in turn gets unpacked for each of the existing item dunders: obj.__getitem__(*(pos1,), **dict(x=x)) obj.__setitem__(value, *(pos1,), **dict(x=x)) obj.__delitem__(*(pos1,), **dict(x=x)) Now: class C: __key_or_index_translator__ = __key_or_index_translator__ def __getitem__(self, *args, **kwargs): ... def __setitem__(self, value, *args, **kwargs): ... def __delitem__(self, *args, **kwargs): ... >>> C()[1, 2, x=1, y=2] TypeError --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/UQOOOJRHE4LEMIZF7UQNTEVZO5H45MSD/ Code of Conduct: http://python.org/psf/codeofconduct/