在python中以交互模式执行多行语句 [英] Executing multi-line statements in python in interactive mode
问题描述
我是 Python 世界的新手,这是我的第一个 Python 程序.我来自 R 的世界,所以这对我来说有点不直观.
当我执行
In[15]:导入数学...:随机导入...: random.random()...: math.sqrt(85)...:
在交互模式下(即在 >>>
提示符下),我得到 math.sqrt()
的输出.我根本没有看到随机数.但是,当我分别执行 random.random()
和 math.sqrt()
时,我确实得到了两个结果.为什么是这样?我如何获得这两个结果?我问这个是因为我有在 R 中选择多行并以交互模式执行它们的习惯,尤其是当我尝试调试代码时.在尝试调试 2000 行代码时,一次只执行一行会很痛苦.
当我在 R 中选择多行然后执行它们时会发生什么:
runif(1,0,1)平方(85)
它会自动解析这些行并生成以下输出:
<代码>>运行(1,0,1)[1] 0.01597949>平方(85)[1] 9.219544
我研究了 SO 并发现 在单行上写多行语句.我不确定如何使用此线程的官方答案.
<小时>这是 PyCharm 上的输出:
导入数学随机导入random.random()数学.sqrt(85)出[15]:9.219544457292887
我正在寻找某种机制来查看随机数和 85 的 sqrt,而无需添加任何分隔符 、
或 ;
,就像我在 R 代码中所做的那样.我很感激任何想法.
预期输入:可以使用 PyCharm 中的使用控制台执行选择"运行代码 [键盘快捷键:Alt + Shift + E]
导入数学随机导入random.random()数学.sqrt(85)
[请注意,我没有使用任何分隔符;"或,"——就像我们在 R 中所做的一样]
预期输出:
random.random()出[16]:0.183145720117748数学.sqrt(85)出[17]:9.219544457292887
您可能只看到 math.sqrt()
的输出的原因之一是因为默认情况下 ipython/jupyter 只会为您提供多行命令最后一行的输出.Python 解释器逐行执行代码,任何像 random.random()
这样没有明确打印的语句都会被评估并丢弃.Ipython/Jupyter 默认 获取最后一行的结果并显示它.我没有使用过 pycharm,但它可能会做同样的事情.要查看 random.random() 的输出,您有两个选择:
1) 使用如下所示的打印语句.
在[1]中:导入数学随机导入打印 random.random()数学.sqrt(5)...:0.145504928627出[1]:2.23606797749979在 [2]:
2) 通过更改 ast_node_interactivity
参数来更改默认行为 此处
In [1]: from IPython.core.interactiveshell import InteractiveShell在 [2]: InteractiveShell.ast_node_interactivity = "all"在 [3]:导入数学随机导入random.random()数学.sqrt(5)...:出[3]:0.9772320535532782出[3]:2.23606797749979
IPython 有多种魔法命令,例如 %edit
和 %load
,它们允许您直接在您喜欢的编辑器中编辑命令或从文件如下.这可以帮助您选择长代码的特定部分并单独运行它们.下面我将上述行保存在一个名为 test_interpreter.py
的文件中.
在 [1]: !cat test_interpreter.py导入数学随机导入打印 random.random()数学.sqrt(5)在 [2] 中:load -r 1-4 test_interpreter.py在 [3]: # %load -r 1-4 test_interpreter.py导入数学随机导入打印 random.random()数学.sqrt(5)...:0.719244573423出[3]:2.23606797749979
默认情况下,unix shell 中的 python REPL 不支持多行命令(据我所知),但您可以使用 ;
终止并使用 \
开始新行code> 是 python REPL 使用的 readline
的转义字符.
在普通的 python 解释器和 ipython 中,您可以直接从编辑器中复制粘贴行,它们将像下面一样单独评估.
#复制粘贴四行>>>导入数学>>>随机导入>>>random.random()0.16039452147586075>>>数学.sqrt(5)2.23606797749979
I am new to the world of Python, and this is my first program in Python. I come from the world of R so this is a bit unintuitive to me.
When I execute
In[15]: import math
...: import random
...: random.random()
...: math.sqrt(85)
...:
in interactive mode (i.e. on >>>
prompt), I get the output of math.sqrt()
. I don't see the random number at all. However, when I execute random.random()
and math.sqrt()
separately, I do get both the results. Why is this? How do I get both the results? I am asking this because I have the habit of selecting multiple lines in R and executing them in interactive mode, especially when I am trying to debug code. Executing only one line at a time will be excruciating when trying to debug, say, 2000-line code.
Here's what happens when I select multiple lines in R and then execute them:
runif(1,0,1)
sqrt(85)
It automatically parses these lines and generates the following output:
> runif(1,0,1)
[1] 0.01597949
> sqrt(85)
[1] 9.219544
I researched on SO and found Write multi-line statement on single line. I am unsure how to use the official answer from this thread.
Here's the output on PyCharm:
import math
import random
random.random()
math.sqrt(85)
Out[15]: 9.219544457292887
I am looking for some mechanism to see a random number and the sqrt of 85 without having to add any separator ,
or ;
, as I did in R code. I'd appreciate any thoughts.
Expected Input: One would run the code using "Execute Selection using Console" in PyCharm [Keyboard Shortcut: Alt + Shift + E]
import math
import random
random.random()
math.sqrt(85)
[Please note that I haven't used any separator ";" or ","--just as we do in R]
Expected Output:
random.random()
Out[16]: 0.183145720117748
math.sqrt(85)
Out[17]: 9.219544457292887
One of the reasons you are probably seeing the output of math.sqrt()
only is because by default ipython/jupyter will only give you output of the last line of a multiline command. Python interpreter executes code line by line and any statement like random.random()
which are not explicitly printed are just evaluated and thrown away. Ipython/Jupyter by default gets the result of the last line and displays it. I have not used pycharm but it probably does the same thing. To see the output of random.random() you have two options:
1) Use a print statement like below.
In [1]: import math
import random
print random.random()
math.sqrt(5)
...:
0.145504928627
Out[1]: 2.23606797749979
In [2]:
2) Change the default behavior by changing the ast_node_interactivity
parameter as answered here
In [1]: from IPython.core.interactiveshell import InteractiveShell
In [2]: InteractiveShell.ast_node_interactivity = "all"
In [3]: import math
import random
random.random()
math.sqrt(5)
...:
Out[3]: 0.9772320535532782
Out[3]: 2.23606797749979
IPython has a variety of magic commands like %edit
and %load
which allows you to directly edit the commands in your favorite editor or load specific lines of code from a file like below. This can help with your requirement of selecting specific sections of your long code and running them separately. Below I have saved the above lines in a file called test_interpreter.py
.
In [1]: !cat test_interpreter.py
import math
import random
print random.random()
math.sqrt(5)
In [2]: load -r 1-4 test_interpreter.py
In [3]: # %load -r 1-4 test_interpreter.py
import math
import random
print random.random()
math.sqrt(5)
...:
0.719244573423
Out[3]: 2.23606797749979
The python REPL in unix shells by default does not support multiline commands (as far as I know of) but you can use ;
to terminate and start a new line with \
which is escape character for readline
which python REPL uses.
>>> import math;\
... import random;\
... random.random();\
... math.sqrt(5)
0.10298483416372617
2.23606797749979
In both normal python interpreter and ipython you can however directly copy paste lines from your editor and they will be evaluated separately like below.
#Copy paste four lines together
>>> import math
>>> import random
>>> random.random()
0.16039452147586075
>>> math.sqrt(5)
2.23606797749979
这篇关于在python中以交互模式执行多行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!