Am 27.01.23 um 20:18 schrieb Chris Angelico:
All you tell us is
what you're attempting to do, which there is *no good way to achieve*.
Fair enough, that is the answer. It's not possible.
Perhaps someone will be inspired to write a function to do it. 😎
See, we don't know what "it" is, so it's hard to write a function
that's any better than the ones we've seen. Using eval() to construct
an f-string and then parse it is TERRIBLE because:
1) It still doesn't work in general, and thus has caveats like "you
can't use this type of quote character"
Exactly my observation as well, which is why I was thinking there's
something else I missed.
2) You would have to pass it a dictionary of variables, which also
can't be done with full generality
Nonsense. I only am passing a SINGLE variable to eval, called "x". That
is fully general.
3) These are the exact same problems, but backwards, that led to
f-strings in the first place
I don't know what you mean by that.
4) eval is extremely slow and horrifically inefficient.
Let me worry about it.
For some reason, str.format() isn't suitable,
I don't understand why you fully ignore literally the FIRST example I
gave in my original post and angrily claim that you solution works when
it does not:
x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "'y'"
This. Does. Not. Work.
I want to pass a single variable as a dictionary and access its members
inside the expression.
but *you haven't said
why*,
Yes I have, see above.
Well, yes. If you asked "how can I do X", hoping the answer would be
"with a runtime-evaluated f-string", then you're quite right - the
answer might not be what you were hoping for. But since you asked "how
can I evaluate a variable as if it were an f-string", the only
possible answer is "you can't, and that's a horrible idea".
"You can't" would have been sufficient. Pity. Your judgement is
unnecessary and, frankly, uncalled for as well. Multiple instances you
claim that you have no idea what I am doing so how would you even begin
to judge a solution as fit or unfit?
Don't ask how to use X to do Y. Ask how to do Y.
You don't have to be angry that my question does not have a solution. I
will manage and so might you.
Cheers,
Johannes
--
https://mail.python.org/mailman/listinfo/python-list