8.8 【绝佳工具 04】选择执行:Execute Selection in Console¶

当你想写一段简单的测试代码时,或许你会这样子
使用 Python Shell 直接写。缺点是没有自动补全。
在 PyCharm 中新开一个文件。缺点是要新创建一个文件,完了后还要删除。
今天再给大家介绍一种新的方法,可以完全避开上面两种方式的缺点。
那就是 Execute Selection in Console
,可以说是 Run in Anywhere
.
只要在当前文件中,写好代码,然后光标选择后,右键点击
Execute Selection in Python Console
或者 使用快捷键 option + shift +
E (windows 上是 alt + shift + E)。

接着 PyCharm 就会弹出一个 Python Console 窗口,然后运行你所选择的代码。

可以发现其中的一个亮点,就是使用这种方法,PyCharm 会自动帮我们处理好缩进(我们选择时,前面有缩进,可是在执行时,会自动去掉前面多余的缩进)