Evaluation of variable as f-string

Peter J. Holzer hjp-python at hjp.at
Sun Jan 29 19:10:50 EST 2023


On 2023-01-29 10:18:00 +0100, Johannes Bauer wrote:
> Am 29.01.23 um 05:27 schrieb Thomas Passin:
> > IOW, perhaps there is a more practical way to accomplish what you want.
> > Except that we don't know what that is.
> 
> Well, I don't know. I pretty much want a generic Python mechanism that
> allows for exactly what f-strings do: execute arbitrary Python snippets of
> code

That exists. Use eval (or exec).

> and format them in one go.

Include an f-string in the code you eval.

> In other words, I want to be able to do things like that, given an
> *arbitrary* dictionary x and a string s

As I wrote before: An f-string isn't a string. It's a grammatical
construct. So you want to execute Python code which is what eval and
exec do.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20230130/8bada8ee/attachment.sig>


More information about the Python-list mailing list