python interactive mode,在Python中进入互动模式

本文介绍了一种在Python中使用交互模式的方法,通过调用code模块的interact函数可以在程序的任意位置进入交互式控制台,检查变量状态并进行调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'm running my Python program and have a point where it would be useful to jump in and see what's going on, and then step out again. Sort of like a temporary console mode.

In Matlab, I'd use the keyboard command to do this, but I'm not sure what the command is in python.

Is there a way to do this?

For instance:

for thing in set_of_things:

enter_interactive_mode_here()

do_stuff_to(thing)

When enter_interactive_mode() calls, I'd like to go there, look around, and then leave and have the program continue running.

解决方案

code.interact() seems to work somehow:

>>> import code

>>> def foo():

... a = 10

... code.interact(local=locals())

... return a

...

>>> foo()

Python 3.6.5 (default, Apr 1 2018, 05:46:30)

[GCC 7.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)

>>> a

10

Ctrl+Z returns to the "main" interpreter.

You can read the locals, but modifying them doesn't seem to work this way.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值