On Sat, Aug 29, 2020 at 07:22:52PM +0100, Paul Moore wrote:

> > Item assignment (ie __setitem__) is the one thing that a function call 
> > can't do. If we want keywords in our __getitem__ and so on commands, then 
> > one route for item assignment is to allow
> >     >>> d[1, 2, a=3, b=4] = 5
> > as valid syntax.
> 
> Again, obvious. But you still haven't given any reason why we would
> want to do that.

See the PEP:

https://www.python.org/dev/peps/pep-0472/

although I think we can eliminate a couple of the use-cases from 
contention, such as changes to built-in dicts and lists.

For example, consider the case where we might have named axes (I think 
it is Todd who wants this for xarray). We want to be able to use the 
same notation for getters, setters and deleters. I don't know the axis 
names xarray uses, so I'm going to make them up:

If you can use subscripting to get an axis:

    obj[7:87:3, axis='widdershins']

then you ought to be able to use subscripting to set or delete an axis:

    obj[15::9, axis='hubwise']
    del obj[2:5, axis='turnwise']
    
(Users of xarray may be able to suggest some more realist examples.)

I expect pandas could use this too.

I would love to be able to do this in a matrix:

    # standard element access notation
    matrix[2, 3] = 2.5

    # delete an entire column
    del matrix[column=4]

    # and replace an entire row
    matrix[row=3] = [1.5, 2.5, 3.5, 4.5, 6.5]

Of course we could use named getter, setter and deleter methods for 
this, but subscript syntax is a succinct notation which comes very 
close to the standard notation used in the relevant domains.

The bottom line here is that if your operations come in sets of three, 
for getting, setting and deleting a specific sub-element (a row, a 
column, item, key, etc.) then subscript syntax is a more natural 
notation than separate getter/setter/deleter methods.

Even if your object is immutable and so only the getter is defined, it's 
still more natural to use subscript notation.


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

Reply via email to