samedi 21 février 2015

python access variable by name-as-string -inverse eval

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

Aucun commentaire:

Enregistrer un commentaire