Python 在 Spyder 的变量资源管理器中查看局部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24806392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Viewing Local Variables in Spyder's Variable Explorer
提问by Mike L
I'm new to python and am using Spyder's IDE. One feature I appreciate about it is it's variable explorer. However, based on some research, I found that it only shows global variables. A workaround that I found for that is by using the inspect module:
我是 python 新手,正在使用 Spyder 的 IDE。我欣赏它的一个功能是它的变量资源管理器。但是,根据一些研究,我发现它只显示全局变量。我为此找到的解决方法是使用检查模块:
import inspect
local_vars = {}
def main():
global local_vars
a = 2
b = 4
c = a+b
local_vars = inspect.currentframe().f_locals
return c
main()
This works well, however, I have other functions that are called from within main() and I'd like to see those variables in the variable explorer as well. I mimicked what was done for the variables in the main function and the dict does not appear. I noticed that when I disable the setting to "exclude unsupported data types" in Spyder's variable explorer options, the second dict appears with the right size attribute, however, I am unable to open/view it. Any ideas on a possible work around? This is my first time posting BTW.
这很有效,但是,我有从 main() 中调用的其他函数,我也想在变量资源管理器中查看这些变量。我模仿了对 main 函数中的变量所做的事情,但没有出现 dict。我注意到,当我在 Spyder 的变量资源管理器选项中禁用“排除不受支持的数据类型”的设置时,第二个 dict 出现了正确的大小属性,但是,我无法打开/查看它。关于可能的解决方法的任何想法?这是我第一次发布BTW。
Thanks!!
谢谢!!
Here is a working example of my issue and I've traced it down to pylab subplots.
这是我的问题的一个工作示例,我已将其追溯到 pylab 子图。
import inspect, pylab
mainVars = {}
def main():
global mainVars
a = 1
b = 2
fig = pylab.figure()
subPlot = fig.add_subplot(211) ## line of interest
pylab.close('all')
mainVars = inspect.currentframe().f_locals
main()
When the line of interest is commented out, the dict is created successfully and can be viewed. It appears that the object created using fig.add_subplot() is not being handled properly by the dict. It seems to be an unsupported datatype.
注释掉感兴趣的行后,dict就创建成功了,可以查看了。似乎字典没有正确处理使用 fig.add_subplot() 创建的对象。它似乎是不受支持的数据类型。
Hope this helps clarify the issue.
希望这有助于澄清问题。
Thanks again.
再次感谢。
采纳答案by Carlos Cordoba
To view the contents of local variables when some of them are unsupported, you have to follow these steps:
要在其中一些不受支持时查看局部变量的内容,您必须按照以下步骤操作:
Go to the options menu of the Variable Explorer (the last icon from left to right on it).
Select the option called Exclude unsupported data types.
转到变量资源管理器的选项菜单(从左到右的最后一个图标)。
选择名为Exclude unsupported data types的选项。
Then you'll see all local variables saved in the f_locals
dict, even if you're unable to double click on it.
然后您将看到保存在f_locals
字典中的所有局部变量,即使您无法双击它。
回答by chthonicdaemon
All of these workarounds are making your code significantly harder to read for outsiders. You have two options to inspect the values of the variables inside your function. First, you could just return the variables you are interested in:
所有这些变通方法都使您的代码对于外人来说更难阅读。您有两个选项可以检查函数内变量的值。首先,您可以只返回您感兴趣的变量:
def main():
a = 2
b = 4
c = a+b
return a, b, c
a, b, c = main()
Second, if you just want to check that the function works as expected or debug it, you can debug the function and step into it. So select Run|Debug from the menu instead of running the file directly. Then you can step into the function - the values of the variables will be visible in the variable explorer when the execution is inside the function.
其次,如果您只是想检查该函数是否按预期工作或对其进行调试,则可以调试该函数并单步执行。所以从菜单中选择 Run|Debug 而不是直接运行文件。然后您可以进入函数 - 当执行在函数内部时,变量的值将在变量资源管理器中可见。