python 查看变量值,在运行时查看python中变量的所有值

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

then I will see that

x=0

x=1

x=2

x=3

appearing in the window.

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

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

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

解决方案

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.

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

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:

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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值