News for hackers. Stuff that matters.
Re:PEP 750 – Template Strings (t-strings) have been accepted by miki123211 on Thursday April 10, 2025 @09:20PM (#43649518)
In many languages, f-strings (or f-string like constructs) are only supported for string literals, not user-supplied strings.

When compiling, those can be lowered to simple string concatenation, just like any for loop can be lowered to and represented as a while.

Re:PEP 750 – Template Strings (t-strings) have been accepted by zahlman on Thursday April 10, 2025 @10:18PM (#43649876)
In case there was confusion: Python's f-string functionality in particular is specific to string literals. The f prefix doesn't create a different data type; instead, the contents of the literal are parsed at compile time and the entire thing is rewritten into equivalent string concatenation code (although IIRC it uses dedicated bytecodes, in at least some versions).

The t-string proposal involves using new data types to abstract the concatenation and formatting process, but it's still a compile-time process - and the parts between the braces still involve code that executes first - and there's still no separate type for the overall t-string literal, and no way to end up eval'ing code from user-supplied data except by explicitly requesting to do so.

Re:PEP 750 – Template Strings (t-strings) have been accepted by the_clarence on Thursday April 10, 2025 @11:40PM (#43650297)
There is no compile time in python
Re:PEP 750 – Template Strings (t-strings) have been accepted by zahlman on Thursday April 10, 2025 @11:54PM (#43650365)
Yes, there is.

Python source code is translated into bytecode for a VM just like in Java or C#, and by default it's cached in .pyc files. It's only different in that you can ask to execute a source code file and the compilation happens automatically before the bytecode-interpretation.

`SyntaxError` is fundamentally different from other exceptions because it can occur during compilation, and only occurs at run-time if explicitly raised (or via explicit invocation of another code compilation, such as with `exec`/`eval`, or importing a module). This is also why you can't catch a `SyntaxError` caused by the invalid syntax of your own code, but only from such an explicit `raise` or a request to compile a source code string (see https://stackoverflow.com/questions/1856408 ).

Re:PEP 750 – Template Strings (t-strings) have been accepted by pansa2 on Thursday April 10, 2025 @11:43PM (#43650315)
Yes there is, when it compiles source code to bytecode.
Re:PEP 750 – Template Strings (t-strings) have been accepted by mjevans on Thursday April 10, 2025 @10:05PM (#43649790)
My reply was to the parent post's SPECIFIC example of Golang's rejected feature request. Please go read that proposal.

It is NOT about the possibility of referencing existing / future (lazy / deferred evaluation) string literals from within the string, but about a format string that would literally evaluate arbitrary functions within a string.

Re:PEP 750 – Template Strings (t-strings) have been accepted by unscaled on Thursday April 10, 2025 @10:25PM (#43649918)
The proposal doesn't say anything about executing code in user-supplied strings. It only talks about a string literal that is processed by the compiler (at which point no user-supplied string can be available).

On the other hand, the current solution offered by Go (fmt.Sprintf) is the one who supports a user-supplied format String. Admittedly, there is a limited amount of damage that could be done this well, but you can at the very least cause a program to panic.

The reason for declining this feature[1] has nothing to do with what you stated. Ian Lance Taylor simply said: "This doesn't seem to have a big advantage over calling fmt.Sprintf" and "You can a similar effect using fmt.Sprint". He conceded that there are performance advantages to string interpolation, but he doesn't believe there are any gains in usability over fmt.Sprintf/fmt.Sprint and as is usual with Go (compared to other languages), they're loathe to add new features to the compiler[2].

[1] https://github.com/golang/go/issues/34174#issuecomment-14509...

[2] https://github.com/golang/go/issues/34174#issuecomment-53013...

Re:PEP 750 – Template Strings (t-strings) have been accepted by NoTeslaThrow on Thursday April 10, 2025 @09:22PM (#43649529)
What's the risk of user supplied strings? Surely you know their size. What else is there to worry about?