0

I have the following dict defined

AMTVALUE1 = "string 1"
AMTVALUE2 = "string 2"

all_templates = {
    "amt": AMTVALUE1,
    "cmvb": AMTVALUE2,    
}

when i do all_templates["amt"] output is string 1 as expected.

I would like to know if there is a way to access the actual variable name that is being used for the key value pair? So instead of outputting string 1, I would like to output the variable name that is used for the value.

the output would be AMTVALUE1 or AMTVALUE2

iambdot
  • 887
  • 2
  • 10
  • 28
  • 1
    No there isn't. Why do you need this? It smells suspiciously like an [X-Y Problem](//meta.stackexchange.com/q/66377/174780) – Pranav Hosangadi Sep 24 '21 at 19:06
  • 2
    There could possibly be a way to do that, but it's terrible practice to have your logic rely on the names of variables. This is a clear case of the [XY Problem](https://meta.stackexchange.com/a/66378/858527). – Random Davis Sep 24 '21 at 19:06
  • 1
    Once you put the value into the dictionary, it has no connection to the original variable that was used. – Barmar Sep 24 '21 at 19:10
  • @PranavHosangadi I wanted to use the variable name to log it instead of the key – iambdot Sep 24 '21 at 19:11
  • Save it into the dictionary explicitly. – Barmar Sep 24 '21 at 19:12
  • 3
    Maybe something like `"amt": {"name": "AMTVALUE1", "value": AMTVALUE1}` – Barmar Sep 24 '21 at 19:12

0 Answers0