Evaluation of variable as f-string

Peter J. Holzer hjp-python at hjp.at
Sat Jan 28 09:22:18 EST 2023


On 2023-01-27 21:31:05 +0100, Johannes Bauer wrote:
> > But if you really REALLY know what you're doing, just use eval()
> > directly.
> 
> I do, actually, but I hate it. Not because of the security issue, not
> because of namespaces, but because it does not reliably work:
> 
> >>> s = "{\"x\" * 4}"
> >>> eval("f'" + s + "'")
> 'xxxx'

That's exactly the result I expected. What did you expect?

        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/20230128/30d3d5e8/attachment.sig>


More information about the Python-list mailing list