gpt4 book ai didi

python - 在执行函数时进入 python 解释器

转载 作者:IT老高 更新时间:2023-10-28 21:11:42 26 4
gpt4 key购买 nike

我有一个带有函数的 python 模块:

def do_stuff(param1 = 'a'):
if type(param1) == int:
# enter python interpreter here
do_something()
else:
do_something_else()

有没有办法进入我有评论的命令行解释器?这样如果我在 python 中运行以下命令:

>>> import my_module
>>> do_stuff(1)

我在 do_stuff() 中有注释的范围和上下文中得到下一个提示?

最佳答案

如果您想要一个标准的交互式提示(而不是调试器,如 prestomation 所示),您可以这样做:

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

见:code module .

如果您安装了 IPython,并且想要一个 IPython shell,您可以为 IPython >= 0.11 执行此操作:

import IPython; IPython.embed()

或者对于旧版本:

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell(local_ns=locals())

关于python - 在执行函数时进入 python 解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com