1

Is it possible in any way to affect an accessible variable, if the only thing given about it is its name as a string??

>>> class a_class:
        x = 1
>>> string = 'a_class.x'
>>> eval(string)
1
>>> y = inverse_eval(string)
>>> y = 2 #would imply that a_class.x = 2 from now on

1 Answer 1

2

Here is one method:

>>> c, v = string.split('.')
>>> setattr(locals()[c], v, 2)

We can verify that it worked:

>>> a_class.x
2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.