On Fri, Sep 18, 2020 at 10:38 AM Steven D'Aprano <[email protected]> wrote:
>
> On Fri, Sep 18, 2020 at 09:05:39AM +1000, Chris Angelico wrote:
>
> > > f-strings are not literals. They are a form of eval().
> >
> > Oh, that makes 'em sound way too scary :) They're more akin to a list
> > display - a syntactic structure that yields a well-defined object, and
> > has expressions inside it.
>
> *shrug*
>
> Okay, but either way they certainly aren't a literal. Anyone who doubts
> that ought to disassemble an f-string and see for themselves:
>
> import dis
> code = compile("f'{x+1} {x-1}'", '', 'eval')
> dis.dis(code)
>
Agreed. The advantage of them being a syntactic structure (rather than
some sort of masked eval() call) is that the proposal at hand DOES
make sense. Consider:
# List display (or tuple w/o the brackets)
x = [spam, ham]
# Unpacking assignment (ditto)
[spam, ham] = x
# Formatted string
txt = f"foo bar {qty:d} quux"
# Proposed string unpacking
f"foo bar {qty:d} quux" = txt
It doesn't make sense to assign to a function call. It does make sense
to assign to a construct that is handled by the parser. We have
assignment forms that look just like lookups ("x = a[1]" and "a[1] =
x").
But yes, all of this is also predicated on f-strings NOT being literals.
ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/HTO7BQTMDWMTVHMMIAIEHZ4PSJWTEL3N/
Code of Conduct: http://python.org/psf/codeofconduct/