I'm opening this thread to discuss and collect feedback on a language change to 
support keyword arguments to be self-assigned using variables names.

Proposal
--------

Taking the syntax from [bpo-36817](https://bugs.python.org/issue36817) which 
just [made it to Python 
3.8](https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging)
 the `<keyword name>=` syntax would be valid and have the the same effect as 
`<keyword name>=<keyword name>`, so these two statements would be equivalent:

```python
foo(bar=bar, qux=qux)
foo(bar=, qux=)
```

This syntax would only be valid inside functions calls so all of the following 
would remain invalid syntax:

```python
x = 42
x=
# SyntaxError
```

```python
x: Any
[x= for x in foo]
# SyntaxError
```

```python
x = 42
def foo(x=):
    ...
# SyntaxError
```

The self-documenting variables in f-strings would also remain unchanged as it 
is not related to function calls:

```python
x = "foo"
f"{x=}"
# x=foo
```

```python
x = "..."
f"{foo(x=)=}"
# foo(x=)=...
```

Also, this proposal does not intent to extend this syntax in any form to the 
dictionary literal declaration syntax. Although possible, language changes to 
support these are not being discussed here:

```python
x = "..."
foo["x"] =
# just as stated previously, SyntaxError still
```

```python
x, y = 1, 2
foo = {
    x=,
    y=,
}
# SyntaxError
```

Worth noting that the following would now be possible with the proposed syntax:

```python
x, y = 1, 2
foo = dict(
    x=,
    y=,
)
```

Rationale
---------

Keyword arguments are useful for giving better semantics to parameters being 
passed onto a function and to lift the responsibility from the caller to know 
arguments order (and possibly avoid mismatching parameters due to wrong order).

Said that, keyword and keyword-only arguments are extensively used across 
Python code and it is quite common to find code that forwards keyword 
parameters having to re-state keyword arguments names:

```python
def foo(a, b, c):
    d = ...
    bar(a=a, b=b, c=c, d=d)
```

Of course if `bar` arguments are not keyword-only one could just omit keywords, 
i.e. `bar(a, b, c, d)`. Though now the caller has to be aware of arguments 
ordering again. And yet, if arguments are keyword-only then there is nothing 
you can do. Except for relying on varkwargs:

```python
def foo(**kwargs):
    kwargs["d"] = ...
    bar(**kwargs)
```

Which, beyond having its own disadvantages, defeats its own purpose of avoiding 
repeating or reassigning the keys real quickly if one has to work with these 
arguments in `foo` before calling `bar`:

```python
def foo(**kwargs):
    kwargs["d"] = kwargs["a"] * kwargs["b"] / kwargs["c"]
    bar(**kwargs)
```

With all that's being said, introducing self-assigned keywords would be useful 
for a wide range of Python users while being little intrusive on the Python 
syntax and backwards compatible.

Best regards,
Rodrigo Martins de Oliveira
_______________________________________________
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/SIMIOC7OW6QKLJOTHJJVNNBDSXDE2SGV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to