On Mon, Aug 3, 2020 at 5:23 PM Todd <[email protected]> wrote:
> Another approach could be too simply pass the labelled indices in a dict
> as a third/fourth positional argument.
>
> So for indexing
>
> b = arr[1, 2, a=3, b=4]
>
> Instead of
>
> __getitem__(self, (1, 2), a=3, b=4)
>
> Just do
>
> __getitem__(self, (1, 2), {'a': 3, 'b': 4})
>
Steven D's reply to me in the previous thread explains why this isn't a
great approach; I'll copy and paste the relevant section.
Summary: having to "break up" the positional dictionary is painful, for not
much of a benefit.
His comment was actually more specific to Jonathan Fine's key object idea,
but it applies just as much to this API suggestion, I think.
Go through the exercise. I have -- I've written Python 2 code that
>
needed to handle-keyword only arguments, and this was the only way to do
> so.
>
> The "only one parameter, which may receive a keyobject" design will
> have us writing code something like this:
>
>
> # I want this: def __getitem__(self, item, * a, b, c, d=0)
> # but have to write this:
>
> def def __getitem__(self, item):
> # Determine whether we got any keyword arguments.
> if isinstance(item, keyobject):
> keys = item
> item = ()
>
> elif isinstance(item, tuple):
> # Assuming that all keyword args are at the end;
> # if there could be more than one keyobject, or if
> # they could be anywhere in the tuple, this becomes
> # even more complex. I don't even want to think
> # about that case.
> if item and isinstance(item[-1], keyobject):
> keys = item[-1]
> item = item[:-1]
>
> else:
> keys = keyobject()
>
> # Now extract the parameters from the key object.
> if 'a' in keys:
> a = keys.pop('a')
> else:
> raise TypeError('missing keyword argument "a"')
> # same for b and c
>
> d = keys.pop('d', 0)
> # Check for unexpected keywords.
> if keys:
> raise TypeError('unexpected keyword')
>
>
> (Any bugs in the above are not intentional.) And now finally we can
> actually use the keyword parameters and write the method.
>
However I'll also point out that another idea from Jonathan Fine has the
potential to fix both this problem and the key object signature problem,
which is what he called a "SIGNATURE CHANGING ADAPTER".
> Here's how it goes. First we write
> class D:
> @wibble
> def __setitem__(self, val, u, v, x, y):
> pass # Or do something.
>
> Next, we define wibble. It will be a SIGNATURE CHANGING ADAPTER. Those
> who know how to make decorators will, I hope, have little difficulty in
> defining wibble to do what is required. For this exercise, assume that
> k.argv = (1, 2), and k.kwargs = dict(x=3, y=4).
>
>
---
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 -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/HRYXHSV66C5YVKGL7YFUHEN74DB7DXCP/
Code of Conduct: http://python.org/psf/codeofconduct/