在PyCharm中的IPython中出现奇怪的“print”行为

3

print函数在显示时似乎会减少点号的数量:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.22.0
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux

In [2]: s = "A\n..... a\n..... b\n"

In [3]: print(s)
A
.. a
.. b


In [4]: 

终端里不会有这样的事情发生:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "A\n..... a\n..... b\n"
>>> print(s)
A
..... a
..... b
>>> 

即使我在其中运行IPython:
$ ipython

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: s = "A\n..... a\n..... b\n"

In [2]: print(s)
A
..... a
..... b


In [3]: 

有人知道这种行为的原因吗?

1
为什么print(即三个点)会打印空白? - P2000
1个回答

3

我认为这与iPython隐式行连接的语法有关。PyCharm可能会将三个点转换为省略号,并将其解释为“新行命令”(就像在ipython控制台中,您可以在不关闭括号的情况下换行并期望其他参数一样)。


2
这篇Reddit帖子提供了相同的答案,但那只是猜测... - FedericoCozziVM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接