On Sun, Sep 27, 2020 at 07:59:18AM -0300, Sebastian Kreft wrote:

> Hi Steven, could you share some examples of what you have in mind. Having a
> more concrete example of an API that would benefit from mixed-subscripting
> would allow us to better understand its usefulness.

I have an experimental Matrix class:

https://en.wikipedia.org/wiki/Matrix_(mathematics)

There are (at least) three indexing operations needed:

- row
- column
- individual cell

The first two support get, set and delete; the last supports 
only get and set.

One obvious API would be a keyword to disambiguate between the first two 
cases:

    matrix[3, 4]           # unambiguously a cell reference
    matrix[3]              # ambiguous, forbidden
    matrix[3, axis='row']  # unambiguously a row
    matrix[3, axis='col']  # unambiguously a column

These could be supported for all of get, set and delete (except for 
cells) operations. A quick sketch of the implementation with minimal 
error checking for brevity:


    def __setitem__(self, index, value, *, axis=None):
        if isinstance(index, tuple):
            # Operate on a cell.
            if axis is not None:
                raise TypeError('cell ops don't take axis keyword')
            i, j = index
            ... # bind a single cell

        elif isinstance(index, int):
            if axis == 'row':
                ... # bind the row
            elif axis == 'col':
                ... # bind the column
            else:
                raise ValueError('bad axis')

        else:
            raise TypeError


-- 
Steve
_______________________________________________
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/CGMHA4LIE6EIFJWLQ5SEMLQWPKRCIY2V/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to