I want to automatically define variables using a dynamic string, similar to ${$a}
in PHP.
As I do this in a plain script I do not have objects and therefore can not apply setattr
or am I wrong?
How do I dynamically define variables?
I want to automatically define variables using a dynamic string, similar to ${$a}
in PHP.
As I do this in a plain script I do not have objects and therefore can not apply setattr
or am I wrong?
How do I dynamically define variables?
You can store the variables you want in a dictionary. That's basically what php does, only in Python you can specify which dictionary to use and how to handle it.
You can modify local or global dictionary:
>>> locals()['xxx'] = 'Hi'
>>> xxx
'Hi'
>>> globals()['yyy'] = 'Hello'
>>> yyy
'Hello'