I bring a strong proposal that is prematurely rejected but indeed the most likely to be accepted to our community. That is to select as
spelling not :=
, and modify with
and except
semantics at the same time. For that, we need to investigate what will be derived from the change throughly and here are the results.
This alternative has already been out of consideration, PEP 572 says, but is still here because many Python developers are wondering if as
spelling can go and there are few discussions about the way of modifing the two semantics. Even PEP 572 shows little attension to this possibility.
https://docs.python.org/3/reference/grammar.html
Current as
semantics is not consistent (or is in a way that it is defined depending on each statement).
TODO
=
assignment statement
# not independently defined
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
('=' (yield_expr|testlist_star_expr))*)
annassign: ':' test ['=' test]
import
statement
import_stmt: import_name | import_from
import_name: 'import' dotted_as_names
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
'import' ('*' | '(' import_as_names ')' | import_as_names))
import_as_name: NAME ['as' NAME]
dotted_as_name: dotted_name ['as' NAME]
import_as_names: import_as_name (',' import_as_name)* [',']
dotted_as_names: dotted_as_name (',' dotted_as_name)*
dotted_name: NAME ('.' NAME)*
with
statement
with_stmt: 'with' with_item (',' with_item)* ':' suite
with_item: test ['as' expr]
except
clause
except_clause: 'except' [test ['as' NAME]]
# This
STATEMENT(EXPR_0 as NAME_0, EXPR_1 as NAME_2, ..., EXPR_n as NAME_n)
# can always be replaced by
NAME_0 = EXPR_0
NAME_1 = EXPR_1
...
NAME_n = EXPR_n
STATEMENT(NAME_0, NAME_1, ..., NAME_n) # TODO STATEMENT(EXPR_0, EXPR_1, ..., EXPR_n) is better?
# except `import` statement since it's special originally which means no `EXPR` cannot be the left hand of `as` in its statement but simply `NAME`: `import NAME_0 as NAME_1`
# This interpretation above is valid no matter how `EXPR_i` uses `NAME_j` (i > j).
# When you write
EXPR as NAME_0, NAME_1
# it's interpreted as
(EXPR as NAME_0), NAME_1
# You can do unpacking by this
EXPR as (NAME_0, NAME_1)
# `EXPR as NAME` itself can be `EXPR` and it returns just `EXPR` in `EXPR as NAME` which means you can write
((EXPR as NAME_0) as NAME_1) ... as NAME_n
# or simply like this even at the top level since it's determininable.
EXPR as NAME_0 as NAME_1 ... as NAME_n
NAME_0 = EXPR as NAME_1 ... as NAME_n
# Also you can write
f(x=EXPR as NAME)
# since this is valid and `EXPR as NAME` can be `EXPR`.
f(x=EXPR)
# And also this is passible.
if (EXPR as NAME).method(): ...
# The `EXPR` in `EXPR as NAME` is matched as long as possible which means
if 0 < 1 as x: ...
# is interpreted as
if (0 < 1) as x: ...
# not
if 0 < (1 as x): ...
# but also `EXPR` is separated by `,` which means
EXPR_0, EXPR_1 as NAME
# is interpreted as
EXPR_0, (EXPR_1 as NAME)
# rather than
(EXPR_0, EXPR_1) as NAME
# TODO even when used `as` assignment expression in list comprehension,
# you can apply the same rules above first by putting it to `for` loop form.
# There is no equivalence to type annotation and augmented assignment.
TODO
- Mutable / imutable objects and call by object mechanism
TODO The statements below should be allowed?
import NAME_0 as NAME_1
import NAME_0 as NAME_1.NAME_2
import NAME_0 as NAME_1[n]
from MODULE import NAME_0 as *NAME_1 # when NAME_0 is iterable
with
statement will no longer responsible for passing returned value from__enter__
to the right hand ofas
in its statement and merely call__enter__
when enter the statement and__exit__
when exit from it.- Context manager can be renamed simply to context since it will no longer be manager but context itself. Context object has status of the context and encapsulates it.
except
statement will no longer responsible for passing instance of the right hand ofas
in its statement.- Exceptions must be instanciated and also it must be confirmed otherwise
except
statement could rewrite the class globally.
TODO Converter to be provided.
https://lwn.net/Articles/749200/