Dear Developers,

After some findings and further thoughts through this thread, I have
formulated a proposal as follows.  (I thank everyone who has commented
in this thread).  For the necessary definitions, please look at

https://docs.python.org/3/reference/expressions.html#subscriptions
https://docs.python.org/3/reference/expressions.html#expression-lists
https://docs.python.org/3/reference/expressions.html#slicings

Difference from PEP 646 will be explained in a remark to follow.


Note: In view of the definition of an expression statement and that of
an assignment statement

https://docs.python.org/3/reference/simple_stmts.html#expression-statements
https://docs.python.org/3/reference/simple_stmts.html#assignment-statements

I believe the correct definition of a starred expression actually used
is

  starred_expression ::=  expression | (starred_item ",")+ [starred_item]

(that is, use "+" instead of "*").


Issues treated
--------------
(1) The Language Reference is currently not describing the actual
    treatment of starred expressions precisely.  For instance, the
    Language Reference says

      return_stmt ::=  "return" [expression_list]

    at

    https://docs.python.org/3/reference/simple_stmts.html#the-return-statement

    but things like

      return *x, *y

    are accepted, which makes good sense and should be an officially
    supported use I think, but is not documented (in the Language
    Reference at least).

(2) The rules on subscription of an object is not as simple as it can
    be.  For instance, things like "a[*x,]" or "a[]" raises
    SyntaxError possibly unexpectedly.


Proposal
--------
(Z) Correct the definition of a starred expression in the Language
    Reference.

(A) Syntax
    ------
    Simplify the syntax as described in the Language Reference in the
    following manner.

    (A1) Remove the definition of "expression_list".

    (A2) Then replace every occurrence of it in the specification of
         the syntax with "starred_expression".  In particular, change
         the definition of subscription to

       subscription ::=  primary "[" starred_expression "]"

    Semantics
    ---------
    Reduce the interpretation of resulting newly valid expressions and
    statements to that of previously valid ones in the obvious manner.
    That is, interpret them as if each of the starred expressions in
    place of an expression list were packed in parentheses just around
    it.

(B) Syntax
    ------
    Change the definition of subscription further to

      subscription ::=  primary "[" [starred_expression] "]"

    (meaning a starred expression there is now optional).

    Semantics
    ---------
    For subscription, without changing the meaning of the previously
    valid expressions, make the pair "[", "]" and the pair "[(", ")]"
    interchangeable.  That is, interpret the brackets "[" and "]" for
    subscription as if "[" is a shorthand for

      .__getitem__((

    (or

      .__class_getitem__((

    depending on the object in front of it) and "]" is a shorthand for

      ))

    at the time of evaluation, and similarly at assignment and
    deletion in the obvious appropriate way using the methods
    __setitem__ and __delitem__ respectively.

(C) Syntax
    ------
    Change the definition of a slicing to

      slicing      ::=  primary "[" [slice_expression] "]"
      slice_expression ::=  slice_item | (starred_slice_item ",")+
[starred_slice_item]
      starred_slice_item   ::=  starred_item | slice_item

    Semantics
    ---------
    Reduce the interpretation of a slicing to that of a subscription
    in the usual way, namely, by replacing every item between the
    brackets which is a proper slice with an expression for the
    corresponding slice object.


Remarks
-------
(1) The step (A) would in fact do most of the step (B).  For instance,
    after the step ((Z) and) (A), "a[*x,]" would be a valid expression
    equivalent to the already valid expression

      a[(*x,)]

    satisfying (B).  The only thing (B) would add to (A) in concrete
    terms is it would make

      a[]

    a valid expression equivalent to "a[()]" (where "a" may of course
    be replaced by any Python "primary").

(2) The proposal disagrees with PEP 646 in that, after our steps, the
    expression of the form "a[*x]" will not get valid.  In particular,
    unlike with PEP 646, we won't have e.g.,

      a[*[y]]

    as a valid expression which is _not_ equivalent to

      a[y]

    The details of PEP 646 on the subject can be found at

    https://www.python.org/dev/peps/pep-0646/#star-expressions-in-indexes

    (In either way, "a[*[y],]" would be valid and equivalent to
    "a[y,]".)


Thats' it.  I hope to hear your thoughts.

Best regards,
Takuo Matsuoka
_______________________________________________
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/BEGGQEU6MG7RYIY7HB4I6VQ23L6TXB6H/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to