On Wed, Sep 30, 2020 at 2:44 AM Steven D'Aprano <steve@pearwood.info> wrote:
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
Have you considered using matrix[row=3], matrix[col=3]? In that case it would be a keyword only access. What advantages do you see with your current API? Or alternatively, using numpy's current syntax matrix[3, :], matrix[:, 3] (maybe `...` could be another option, if `:` is too magic)
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-leave@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/CGMHA4... Code of Conduct: http://python.org/psf/codeofconduct/
-- Sebastian Kreft