如何将Python代码组织成可折叠/可扩展的块?

2024-04-20 12:02:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在Pycharm中,有一个“code structurre”侧栏,它提供了一个树来浏览代码,但是,它只在代码有类、方法和对象时才有用。如果代码中没有这些内容,那么它就是无用的。 我的问题是:有没有任何方式,我可以命令这是一个块,我想能够折叠它和扩展它?类似于Jupyter的东西,代码本质上被划分为单元。你知道吗

目前,我正在做:

# ---------------------------------- chunck x blah blah -----------------------

编辑: 大多数评论都说我很笨,不知道如何高效地编写代码,我应该使用函数和类。伙计们,我知道怎么用,这不是我的问题。谢谢。你知道吗


Tags: 对象方法代码命令内容方式codejupyter
2条回答

结果发现答案很简单: 选择代码,右键单击,进行自定义折叠

enter image description here

PyCharm允许您在启用“科学模式”时定义“代码单元”。这些代码单元是可折叠和可扩展的。引用PyCharm website

A “code cell” is a block of lines to be executed all at once in the integrated Python console. You can define cells simply by adding inline comments #%% to your regular Python files. PyCharm detects these comments and shows you a special run icon in the left gutter. Clicking this icon triggers the execution of a cell:

enter image description here

唯一需要注意的是,Scientific模式及其代码单元功能仅在PyCharm专业版中可用。你知道吗

相关问题 更多 >