0

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?

Chris_Rands
  • 35,097
  • 12
  • 75
  • 106
lony
  • 5,917
  • 8
  • 55
  • 79

2 Answers2

0

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.

Matthieu Brucher
  • 20,726
  • 7
  • 35
  • 56
-1

You can modify local or global dictionary:

>>> locals()['xxx'] = 'Hi'
>>> xxx
'Hi'
>>> globals()['yyy'] = 'Hello'
>>> yyy
'Hello'
pbacterio
  • 1,011
  • 5
  • 11