r/Python • • Sep 09 '15

Pep 498 approved. :(

https://www.python.org/dev/peps/pep-0498/
285 Upvotes

324 comments sorted by

View all comments

Show parent comments

3

u/stevenjd Sep 10 '15

Calling format(x=expr) isn't a property of format. format is a method, and it takes arguments. You get expressions for free, because that's what the language gives you.

But f"{expr}" supporting arbitrary expressions is a property of f-strings. Here's a hint to tell the difference: do you have to change the parser? If I add a new string method, does the parser have to change to support arbitrary expressions as arguments to the new method? (Answer: no.) If I add a new kind of f-string, apart from the f prefix itself does the parser have to change to support expressions inside the string? (Answer: yes.)

Here I have a string: "{x}". Does that automatically and magically change the string from "x" to the value of the variable x found in some enclosing scope? (Answer: no.) How about f"{x}"? (Answer: yes.) The fact that f-strings automatically and magically interpolate the string with variables and expressions found in the enclosing scopes is the whole point of the feature. You just don't like the term "automagically" because it is generally considered a pejorative term. Well, if the shoe fits, wear it.

1

u/desmoulinmichel Sep 10 '15

New syntax is not automagic. It's just new syntax. Magic happens when you do something very implicitly so it leave you with no context to evaluate what's going and it seems to come out of nowhere. Not the case here.