You can find the object in globals:
def define_var(dictionary, entry, counter):
d_name = next(k for k, v in globals().items() if v is dictionary)
for i in range(counter):
print("reason%d = %s['%s_%d']" % (i + 1, d_name, entry, counter))
print(define_var(result_dict, 'start', 3))
reason1 = result_dict['start_3']
reason2 = result_dict['start_3']
reason3 = result_dict['start_3']
If you print(id(result_dict))
and print(id(dictionary))
in the function when you pass in result_dict
you will see they both are the same object with the same id's so we just use is
to get the name from the global
dict.
If you want to assign the result to a reason variable you can return it, something like:
def define_var(dictionary, entry, counter):
d_name = next(k for k, v in globals().items() if v is dictionary)
return "%s['%s_%d']" % (d_name, entry, counter)
reason = define_var(result_dict,2,3)
print(reason)
Or use a dict:
def define_var(dictionary, entry, counter):
d_name = next(k for k, v in globals().items() if v is dictionary)
reasons = {}
for i in range(counter):
reasons["reason{}".format(i)] = "{}[{}]".format(d_name, entry)
return reasons