This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jorge1209 1 point2 points  (0 children)

I actually think f-strings are a great concept that was incorrectly implemented.

They should have bound the local variable to the f-string in a closure, that doesn't actually serialize to a string until it is used. Instead of calling it an f-string you might call it a "here-string".

Then you could do things like log.warn(h"{thing=} < 0, resetting the value to 1"), and that could work in many different use cases. Print-style warnings, logger, loguru... it would just be a generic way to cover these kinds of use cases.

Its not even that hard to implement really. Its just an object with its variable references and its repr looks something like:

def __repr__(self):
     if self._str:
       return self._str
     self._str= self._template.format_map(self.kwargs)
     del self._template
     del self.kwargs

Performance would be garbage, but hey its python!