在运行时查看python中变量的所有值 [英] Seeing all the values of variables in python as it is run

查看:1966
本文介绍了在运行时查看python中变量的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MatLab中,除非我添加;在该行的末尾,我可以运行并查看此代码的作用.例如,如果我编码

In MatLab, unless I add ; at the end of the line, I can just run and see what this code is doing. For example if I code

x=0
for i=1:1:3
    x=x+1
end

那我会看到的

x=0
x=1
x=2
x=3

出现在窗口中.

这样,我可以看到我的代码的过程,因此可以轻松地修复错误.

That way, I can see the process of my code, and so I can easily fix the error when there is.

如何在Python中做同样的事情?

How can I do the same thing in Python?

我知道我可以添加打印来实现它

I know I can just add print to make it happen like

for i in list
    print('i is:', i)
    i=i+1

但是,这会使整个代码过于混乱.有没有类似MatLab的方法?

But this makes the whole code too messy. Is there a MatLab-like way to do this?

推荐答案

我对matlab并不熟悉,但是根据您的解释,您似乎想跟踪代码以查看错误发生在哪里?您可以通过pdbtrace做到这一点.

I am not familiar with matlab, but from what you are explaining, you seem to be wanting to trace your code to see where errors come up? You can do this through pdbtrace.

让我知道这是否是您想要的.

Let me know if this is what you are looking for.

以下是使用代码示例如何使用pdbtrace的快速方法:

Here is a quick how-to on how to use pdbtrace using your code sample:

import pdb
l = [1, 2, 3, 4]
pdb.set_trace()
for i in l:
    print('i is:', i)
    i = i+1

因此,当您运行此程序时,您将能够控制应用程序的流程.在代码的下一步中,单击"n",您可以看到它在做什么.

So, when you run this, you will then have the ability to control the flow of the app. You hit 'n' for the next step in your code, and you can see what it is doing.

我强烈建议您阅读此书,因为它是一个很好的教程:

I strongly suggest reading this, as it is an excellent tutorial:

https://pythonconquerstheuniverse.wordpress.com/2009/09 /10/debugging-in-python/

此外,我实际上更推荐的是使用具有完整功能调试器的IDE.我是PyCharm的一部分,您可以在此处免费下载: https://www.jetbrains.com/pycharm/download/

Furthermore, what I actually recommend even more, is using an IDE that has a fully functional debugger. I am partial to PyCharm, which you can download for free here: https://www.jetbrains.com/pycharm/download/

这篇关于在运行时查看python中变量的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆