for i in range(10) : var_i = i
It doesn't recognize me like this.
If you write a dictionary, you can tie it with key and value, but
The key value is recognized as str.
Is there a simple way?
python
You can use the reflection function
Python is managed by dict when paired, such as 'name' and 'value', as in variables.
>>> import sys
>>> mod = sys.modules[__name__]
>>> for i in range(10):
... ... setattr(mod, 'var_{}'.format(i), i)
...
>>> var_0
0
>>> var_1
1
© 2024 OneMinuteCode. All rights reserved.