[Python-ideas] Fix the DRY problem (was Re: PEP 501 - i18n with marked strings)
Chris Angelico
rosuav at gmail.com
Sat Aug 15 15:13:22 CEST 2015
On Sat, Aug 15, 2015 at 10:27 PM, Eric V. Smith <eric at trueblade.com> wrote:
> Instead of translating:
> name = 'Eric'
> dog_name = 'Fluffy'
> f"My name is {name}, my dog's name is {dog_name}"
>
> to:
> __i18n__("My name is {0}, my dog's name is {1}").format('Eric', 'Fluffy')
>
> We instead translate it to:
> __i18n__("My name is {name}, my dog's name is
> {dog_name}").format_map({'name':'Eric', 'dog_name':'Fluffy')
>
> The string would be unchanged from value of the f-string. The keys in
> the dict would be exactly the expressions inside the braces in the
> f-string. The values in the dict would be the value of the expressions
> in the f-string.
>
> This solution works for cases where the expressions inside braces are
> either simple identifiers, or are more complicated expressions.
I know it's a ridiculous corner case, but what if an expression occurs
more than once? Will it be evaluated more than once, or will the exact
text of the expression be used as, in effect, a lookup key? With
simple expressions it won't make any difference, but anywhere else in
Python, if you use the same expression twice, it'll be evaluated
twice.
user = "rosuav"
f"You can log in with user name {user} and your provided password, and
your web site is now online at http://{user}.amazinghosting.example/
for all to see. Thank you for using Amazing Hosting!"
This kind of example should definitely be supported, but what about a
function call?
f"... user name {user()} ... http://{user()}.amazinghosting.example/"
Do that in any other form of expression, and people will expect two
calls. With i18n it'd be impossible to distinguish the two, but I'd
still normally expect user() to get called twice.
ChrisA
More information about the Python-ideas
mailing list