Simpler way to create dictionary of separate variables?

前端 未结 27 1937
名媛妹妹
名媛妹妹 2020-11-22 02:42

I would like to be able to get the name of a variable as a string but I don\'t know if Python has that much introspection capabilities. Something like:

>&         


        
27条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 03:13

    In python 3 this is easy

    myVariable = 5
    for v in locals():
      if id(v) == id("myVariable"):
        print(v, locals()[v])
    

    this will print:

    myVariable 5

提交回复
热议问题