如何在 Jython PythonInterpreter 中使用 for 循环?
How to use for-loop in Jython PythonInterpreter?
是否可以以及如何使用exec()
方法在PythonInterpreter
中写下for-loop?
使用 exec()
看起来不错,就像在 Python 命令行中逐行输入交互式,但以下使用 for
语句不起作用:
PythonInterpreter python = new PythonInterpreter();
python.exec("import sys");
python.exec("for p in sys.path:");
python.exec(" print p");
您在单独的行中传递,Python 应该如何分别解析这些行并验证您有有效的代码?
使用一个字符串:
python.exec("for p in sys.path:\n" +
" print p");
import sys
行仍然可以单独传入,因为导入模块的命名空间在 PythonInterpreter
实例中持久化。
本质上,每个传递给exec()
的字符串都必须跟在stmt_list
grammar token之后;复合语句必须完整。
是否可以以及如何使用exec()
方法在PythonInterpreter
中写下for-loop?
使用 exec()
看起来不错,就像在 Python 命令行中逐行输入交互式,但以下使用 for
语句不起作用:
PythonInterpreter python = new PythonInterpreter();
python.exec("import sys");
python.exec("for p in sys.path:");
python.exec(" print p");
您在单独的行中传递,Python 应该如何分别解析这些行并验证您有有效的代码?
使用一个字符串:
python.exec("for p in sys.path:\n" +
" print p");
import sys
行仍然可以单独传入,因为导入模块的命名空间在 PythonInterpreter
实例中持久化。
本质上,每个传递给exec()
的字符串都必须跟在stmt_list
grammar token之后;复合语句必须完整。