On Tue, Oct 20, 2020 at 11:54 PM Steven D'Aprano <st...@pearwood.info> wrote:
> The only advantage, I guess, of f-string like syntax is that the
> variable names can be embedded inside the template string:
>
>     f"{path:s} - {errors:d} errors, {%warnings:d}" = line
>
> but I'm not convinced that's actually an advantage. I think I prefer the
> C-style for this.

It's exactly the same advantage that they have as rvalues, and I think
there's more to be said for it than is obvious at first glance.
Consider that patterns might not be nice tidy things like this; they
might be parsing an obscure sequence of tokens (because, let's face
it, a lot of text out there isn't designed well), so your pattern
might be something like:

f"{path}: {errors:d}/{warnings:d}" = line

or even:

f"{acc},{date},{type},{amount:[0-9.]}
{description},{refnum:12s}{reference}" = line

which is only a very slight exaggeration from the "why the bleep
didn't you just use actual CSV" parsing from one of my actual apps.
(Banks. Banks are bizarre.)

> Sometimes we want a 500 gigawatt nuclear fusion reaction; but sometimes
> we just want a AA battery. This is the AA battery of parsing.
>
> Personally, I think people use more AA batteries than 500GW fusion
> reactors :-)

[citation needed] :-)

> >     - As others have mentioned: could inject variables into locals()
> > making debugging harder.
>
> I'm dubious about that too. I think it would be better to keep the
> parsing separate from the name binding, and use regular assignment for
> that.

Not sure what the concern with "inject[ing] variables" is. The
f-string has to be a literal - it's not a magic object that you can
assign to and new locals appear.

> Yeah, f-strings might be great, but I don't think they are a good match
> for this functionality.
>
> But I would definitely use the functionality.
>

I'd personally prefer sscanf, myself, but I would use this
functionality if it landed too.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Y7VJMYGAUL6GRM7GSH22QCWB52F4BM2E/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to