0

I need to get the variable name instead of the variable value.

I have a situation like this:

Type1 = 1
Type2 = 2

fields = [Type1, Type2]
for field in fields:
   value = field.__name__ #or something like 
   print(value)

Expected result is

Type1 
Type2

Ty for helping

JuConte
  • 513
  • 2
  • 7
  • 18

1 Answers1

2

Though i don't think there is any way (or i don't know) but an easy way is to store them in a dict like this;

my_dict = {'Type1': 1, 'Type2': 2}

Then to get the variable name, you just need to loop over the my_dict and get all the keys and simply use them as you want.

for key, value in my_dict.items():
    print(key)

this will give an output like this;

Type1
Type2
Irfan wani
  • 4,084
  • 2
  • 19
  • 34
  • Type1 and Type2 are passed from url in django... – JuConte Feb 14 '21 at 17:34
  • 1
    I don't think that has any problem with the solution. When you get them, you can first update the my_dict and store them inside it and then apply my solution – Irfan wani Feb 14 '21 at 17:36