Evaluation of variable as f-string
avi.e.gross at gmail.com
avi.e.gross at gmail.com
Sat Jan 28 00:19:21 EST 2023
May I point out that some dynamic situations can in a sense be normalized?
The example below posits a dynamically allocated dictionary during run time.
But why can't you have a placeholder variable name in place and make your
placeholder a link to the dictionary (or other item) before invoking the
existing f-string with the placeholder built-in, rather than trying to
evaluate an F♯ ???
Of course many situations may not have as much of a possible work-around.
But as so many have noted, we never got a really good explanation of what
the OP really wants to do. There have been replies that may be suitable
solutions and some clearly have potential to be security holes if you let
the users dynamically create strings to be evaluated.
In some languages, many of the facets of the language can be passed along as
a function with some name to be used in functional programming techniques
and this can be very useful. The "operator" module can be used for quite a
few things like operator.add or operator.__add__ or operator.concat and many
more. If the logic used to evaluate an f-string (and for that matter the
other string variants like b'..' and r'...') could be encapsulated in a
function like that, it would be potentially usable as in passing something
like dangerous_operator.f_string and a list of strings and having that
return a list of evaluated strings.
The fact that something like this is not known to the people here may hint
that it is not something considered safe to use by amateurs. But then again,
anyone who wants to can use eval() as Chris points out.
Of course, there may be other reasons too. An f-string is evaluated in a
context that may be different if a string is passed along and then looked at
in another context.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Stefan Ram
Sent: Friday, January 27, 2023 4:31 PM
To: python-list at python.org
Subject: Re: Evaluation of variable as f-string
Johannes Bauer <dfnsonfsduifb at gmx.de> writes:
>>Johannes Bauer <dfnsonfsduifb at gmx.de> writes:
>>>x = { "y": "z" }
>>>s = "-> {x['y']}"
>>>print(s.format(x = x))
>>x = { "y": "z" }
>>def s( x ): return '-> ' + x[ 'y' ]
>>print( s( x = x ))
>Except this is not at all what I asked for. The string "s" in my
>example is just that, an example. I want to render *arbitrary* strings "s"
>together with arbitrary dictionaries "x".
I take this to mean that you want to process a dictionary
name, a string and a dictionary that is only specified as
late as at run time.
import string
name = input( 'name of the dictionary? ' ) string_ = input( 'string? ' ) #
"-> {x['y']}"
dictionary = eval( input( 'dictionary? ' )) print( eval( 'f"""' + string_ +
'"""', {name:dictionary} ))
name of the dictionary? x
string? -> {x['y']}
dictionary? { 'y': 'z' }
-> z
--
https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list