Simpler way to create dictionary of separate variables?

前端 未结 27 1939
名媛妹妹
名媛妹妹 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:28

    I uploaded a solution to pypi. It's a module defining an equivalent of C#'s nameof function.

    It iterates through bytecode instructions for the frame its called in, getting the names of variables/attributes passed to it. The names are found in the .argrepr of LOAD instructions following the function's name.

提交回复
热议问题