On Fri, Sep 18, 2020 at 1:38 AM Eric V. Smith <eric@trueblade.com> wrote:
In general, there's no way to start with a format specifier string and from it get the type of the object that it should be applied to. For example, any string without %'s is a valid datetime format specifier (of dubious value, but such is life). Perhaps a better example is decimal vs. float. What if I want %.2f to return a decimal? Would that just not be possible?
So I think you'd have to limit this to a small set of built-in types.
In general, I think overloading f-strings as assignment targets would be confusing. But I've been wrong before.
It doesn't have to be absolutely identical, just as long as it's mostly parallel. Consider C's printf and scanf formats; any whitespace in scanf matches any whitespace, and the integer handlers are always happy to accept a leading plus sign (which has to be specifically requested in printf). Conversely, scanf can be more restrictive, eg %[a-z] which will match some sequence of lowercase ASCII letters. IMO printf notation (what in Python does with str % ...) is better suited to this than Python's .format() notation (which f-strings also use). But the advantages of f-strings would also apply here. Maybe the best way would be the tagged version of percent formatting?
x, y = 1, 2 "==> (%(x)d, %(y)d)" % globals() '==> (1, 2)'
Theory: "==> (%(x)d, %(y)d)" = "==> (1, 2)" could assign x=1 and y=2. (It'd want to have some sort of tag to show what's going on, but not f"..." since it's not an f-string.) PEP 622 pattern matching may be relevant here. ChrisA