How to transform a text into an identifier name?

Asked

Viewed 112 times

1

I would like to be able to receive a string as a parameter and use as an identifier name, create a variable or function with that name (it is not used as a dictionary key). It is possible to do this?

  • What use is that? It seems strange to me, because you, as a developer, have no way of knowing what will be the name of the variable that the user will enter, so how will you use it in the code?

1 answer

2


It’s possible. You need to know eval(), exec() and compile(), but don’t do such a trick.

There is always a better solution unless what you want is to execute an externally entered code, which is still the wrong resource for this, but it is a way to solve, even though it is still a scam.

Even the dictionary is usually gambiarra in most cases, although it would already be a slightly better solution and solves the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.