Dec-17-2017, 02:20 PM
Using
Think of what happen when do
Making a normal visible dictionary is the way to go.
eval
and exec
is for sure not the way to go.Think of what happen when do
pos1 = 1
>>> pos1 = 1 >>> pos1 1Internally is python storing this a dictionary.
>>> globals() {'__name__': '__main__', 'pos': 1,} >>> globals()['pos1'] 1We should not do the tricks and using the
globals()
dictionary.Making a normal visible dictionary is the way to go.
>>> d = {} >>> for i in range(1,9): ... d[f'pos{i}'] = i ... >>> d {'pos1': 1, 'pos2': 2, 'pos3': 3, 'pos4': 4, 'pos5': 5, 'pos6': 6, 'pos7': 7, 'pos8': 8} >>> d['pos1'] 1