Note: this suggestion should echo 
https://mail.python.org/archives/list/python-ideas@python.org/thread/UUFFAI3FZMQRVPDCUPZEOAZCRNXKWFDE/
 but has a bigger scope and completely differs in the way to proceed.

Currently, Python lacks a way to integrate code parts into string; when I say 
code, I don't talk only about identifiers (on which is focusing `nameof` 
suggestion) but also about expression.

Actually, this feature exists in Python but is hidden in a 3.8 [f-string 
feature](https://docs.python.org/3.8/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging).

Expression string should be made accessible for itself. It could be done by 
adding a leading token `:` to the expression. Thus `f"{:a + b}" == "a + b"`. 
Like "normal" formatted expression, it would be handled by IDEs, especially for 
refactoring. The difference with "normal" formatted expression is that the 
expression would not be evaluated.
A use case example:
```python
def foo(bar: int):
    if bar != 42:
        # If `bar` is renamed, error message will be renamed too by the IDE
        raise ValueError(f"{:bar} is not 42")
```
By the way, there will be the following equivalence: `f"{a + b=}" == f"{:a + 
b}={a + b}"`.

The idea is to reuse existing mechanisms.

(Of course the syntax suggestion is arbitrary and can be discussed. I have 
considered a leading token because there are already a lot of modifiers at the 
end of the expression; I have thought about `:` and `!` and my preferences has 
gone to `:`.)

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

Reply via email to