如何在Python中打印变量名?
假设我有一个名为 choice
的变量,它等于 2。我如何访问该变量的名称? 相当于
In [53]: namestr(choice)
Out[53]: 'choice'
用于制作字典的东西。 有一个很好的方法可以做到这一点,但我只是想念它。
编辑:
这样做的原因是这样的。 我正在运行一些数据分析的东西,我用多个参数调用程序,我想在运行时调整或不调整这些参数。 我从格式为“提示输入值时”的 .config 文件中读取了上次运行中使用的参数。
filename
no_sig_resonance.dat
mass_peak
700
choice
1,2,3
当提示输入值时,将显示先前使用的值,并且空字符串输入将使用先前使用的值。
我的问题出现是因为在编写这些值已被扫描到的字典时。 如果需要参数,我运行 get_param 来访问文件并查找参数。
我想我可以通过读取 .config
文件一次并从中生成字典来避免这个问题。 我最初避免这样做的原因……我已不记得了。 更新我的代码的完美情况!
Say I have a variable named choice
it is equal to 2. How would I access the name of the variable? Something equivalent to
In [53]: namestr(choice)
Out[53]: 'choice'
for use in making a dictionary. There's a good way to do this and I'm just missing it.
EDIT:
The reason to do this is thus. I am running some data analysis stuff where I call the program with multiple parameters that I would like to tweak, or not tweak, at runtime. I read in the parameters I used in the last run from a .config file formated as
filename
no_sig_resonance.dat
mass_peak
700
choice
1,2,3
When prompted for values, the previously used is displayed and an empty string input will use the previously used value.
My question comes about because when it comes to writing the dictionary that these values have been scanned into. If a parameter is needed I run get_param
which accesses the file and finds the parameter.
I think I will avoid the problem all together by reading the .config
file once and producing a dictionary from that. I avoided that originally for... reasons I no longer remember. Perfect situation to update my code!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果你坚持的话,这里有一些可怕的基于检查的解决方案。
我希望它能激励您重新评估您遇到的问题并寻找另一种方法。
If you insist, here is some horrible inspect-based solution.
I hope it will inspire you to reevaluate the problem you have and look for another approach.
要回答您原来的问题:
示例:
作为 @ rbright 已经指出无论你做什么,可能都有更好的方法来做到这一点。
To answer your original question:
Example:
As @rbright already pointed out whatever you do there are probably better ways to do it.
如果你试图这样做,那就意味着你做错了什么。 考虑使用
dict
代替。输出:
If you are trying to do this, it means you are doing something wrong. Consider using a
dict
instead.Output:
我建议描述您面临的问题,而不是询问特定解决方案的详细信息; 我想你会得到更好的答案。 我这么说是因为几乎可以肯定有更好的方法来完成您想要做的任何事情。 解决任何语言的问题通常都不需要以这种方式访问变量名。
也就是说,所有变量名都已经在字典中,可以通过内置函数访问 本地变量 和全局变量。 使用适合您正在检查的范围的正确型号。
检查这些字典的少数常见习惯之一是为了简单的字符串插值:
这种事情往往比替代方案更具可读性,即使它需要按名称检查变量。
Rather than ask for details to a specific solution, I recommend describing the problem you face; I think you'll get better answers. I say this since there's almost certainly a better way to do whatever it is you're trying to do. Accessing variable names in this way is not commonly needed to solve problems in any language.
That said, all of your variable names are already in dictionaries which are accessible through the built-in functions locals and globals. Use the correct one for the scope you are inspecting.
One of the few common idioms for inspecting these dictionaries is for easy string interpolation:
This sort of thing tends to be a bit more readable than the alternatives even though it requires inspecting variables by name.
不能,因为 Python 中没有变量,只有名称。
例如:
这两个变量中哪一个现在是正确的变量?
a
和b
之间没有区别。之前有一个类似的问题。
You can't, as there are no variables in Python but only names.
For example:
Which of those two is now the correct variable? There's no difference between
a
andb
.There's been a similar question before.
这样的事情对你有用吗?
在你的例子中:
Will something like this work for you?
And in your example:
通过热切求值,只要您查看变量(换句话说),变量本质上就会变成它们的值。 也就是说,Python 确实有内置的 命名空间。 例如,locals() 将返回一个字典,将函数的变量名称映射到它们的值,而 globals() 对模块执行相同的操作。 因此:
请注意,您不需要导入任何内容即可访问 locals() 和 globals()。
此外,如果一个值有多个别名,则迭代命名空间只会找到第一个别名。
With eager evaluation, variables essentially turn into their values any time you look at them (to paraphrase). That said, Python does have built-in namespaces. For example, locals() will return a dictionary mapping a function's variables' names to their values, and globals() does the same for a module. Thus:
Note that you don't need to import anything to be able to access locals() and globals().
Also, if there are multiple aliases for a value, iterating through a namespace only finds the first one.
对于如何读取配置参数的修订问题,我强烈建议您节省一些时间和精力并使用 ConfigParser 或(我的首选工具)ConfigObj。
它们可以完成您需要的一切,并且易于使用,其他人已经担心如何让它们正常工作!
For the revised question of how to read in configuration parameters, I'd strongly recommend saving yourself some time and effort and use ConfigParser or (my preferred tool) ConfigObj.
They can do everything you need, they're easy to use, and someone else has already worried about how to get them to work properly!