On Thu, Dec 12, 2019 at 11:01:54AM -0800, Andrew Barnert via Python-ideas wrote:

> I don’t see any problem with a[] being the same as a[()]. We already 
> have a[1,] is the same as a[(1,)] rather than a[1]

A comma is not an "index argument seperator". a[1,] is not the same as 
a(1,) where the comma seperates arguments to the function call, and it 
is permitted to end the call with a seperator. It is equivalent to:

    temp = 1,  # A tuple.
    a[temp]

In the same way, a[2,4,8,16] is the same as 

    temp = 2,4,8,16  # A tuple.
    a[temp]

So a[] would be:

    temp =   # A syntax error.
    a[temp]

not an empty tuple.

The status quo is that subscripting syntax `a[ ... ]` requires that the 
subscript (index, key or slice) inside the square brackets be explicitly 
given. Even if you give `__getitem__` a default value for the subscript, 
when using subscript syntax the caller must still provide it explicitly.

Your suggestion roughly corresponds to hard-coding into the interpreter 
the rule that the empty subscript be changed to a default of `()` 
regardless of what default `__getitem__` is given, or whether it is 
given a default at all.

But why have the empty index default to () rather than None or 0 or -1 
or slice(None, None, None) or some other value? That's an arbitrary 
and not a good one.

And why overrule the locally defined default? I think it would be 
confusing to define:

    def __getitem__(self, index=0): ...

and then have `self[]` pass index=() instead of 0.

Even if subscripting honoured the locally defined default, it is still 
an arbitrary choice with no real justification.


> There are presumably historical reasons why it turned out this way, 
> but if you were designing a new language that had tuple and slice and 
> ellipsis indexing like current Python, would you expect [] to be 
> anything other than [()], or find it confusing?

Yes, I would expect it to be a syntax error, because syntactically, a 
blank token (nothing at all) is not how we create empty tuples.


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

Reply via email to