Discuss / Python / 在交互式环境中调用hello.test(),如何传入参数?

在交互式环境中调用hello.test(),如何传入参数?

Topic source

在交互式环境中调用hello.test(),只能得到 ‘Hello, world!'。

但如果想走第二个或第三个逻辑分支,如何传入参数呢?

直接在交互式环境中,输入

hello.test() xxx

hello.test() 'xxx'

都提示语法错误。

如果作为参数传递给test()函数,

hello.test('xxx')

又会提示test函数没有参数,写法不对。

那么,如何才能走通第二个逻辑分支?

xiongdezhi

#2 Created at ... [Delete] [Delete and Lock User]

交互式环境和命令行执行是两种不同的方式,应该是不可以的,argv保存的是命令行参数。

_潘腾

#3 Created at ... [Delete] [Delete and Lock User]

【我是看上面有个兄弟说的 你可以试试:

在此插入代码

import os os.system('hello.py XXX')

这个最后会返回一个0 我查了一下说是0表示运行正常;1表示出现异常。

syaseh

#4 Created at ... [Delete] [Delete and Lock User]

我想到一种方法就是利用os.popen模拟cmd中的命令行 然后输出结果 代码如下,我在windows下测试可行

import os
result = os.popen('python hello.py Michael').readlines()
print(result)

输入结果为:

['Hello, Michael!\n']

  • 1

Reply