将Python模块划分为多个区域

发布于 2024-12-18 02:59:34 字数 144 浏览 0 评论 0原文

在 C# 中,我们可以使用以下方法创建区域:

#region
// some methods
#endregion

有没有办法以类似的方式格式化 python 代码,以便我可以将所有相关方法保留在一个块中?

In C# we can create regions by using

#region
// some methods
#endregion

Is there any way to format python code in similar fashion so that I can keep all my relevant methods in one block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

风吹雨成花 2024-12-25 02:59:34

看起来 PyCharm 有它,请参见此处: https://www. jetbrains.com/help/pycharm/2016.1/code-folding.html#using_folding_comments

对于 Python 文件,支持以下两种样式。您不应该将它们混合在一个文件中。

#<editor-fold desc="Description">
...
#</editor-fold>

或者

#region Description
...
#endregion

Visual Studio 也接受“区域”

Looks like PyCharm has it, see here: https://www.jetbrains.com/help/pycharm/2016.1/code-folding.html#using_folding_comments

For Python files, the following two styles are supported. You should not mix both of them in a single file.

#<editor-fold desc="Description">
...
#</editor-fold>

or

#region Description
...
#endregion

Visual Studio accepts "region" too

扶醉桌前 2024-12-25 02:59:34

通过 Visual Studio 的 Python 工具,您可以使用:

#region My Block of Code
def f1():
    pass
def f2():
    pass
#endregion

然后您可以像在 C# 上一样折叠区域。

With Python Tools for Visual Studio you can use:

#region My Block of Code
def f1():
    pass
def f2():
    pass
#endregion

Then you can fold the regions same way you do on C#.

拥有 2024-12-25 02:59:34

使用 VS Code,您可以简单地创建这样的区域,甚至为区域添加名称:

#region some name
#Some very long code
#endregion

然后可以将其折叠为以下内容:

#region some name...

With VS Code you can simply create regions like this and even add names for the regions:

#region some name
#Some very long code
#endregion

It can be then folded into the following:

#region some name...
空心空情空意 2024-12-25 02:59:34

我建议您查看 PyDev。如果你的 Python 代码结构良好,那么文档大纲和代码折叠将会非常有用。不幸的是,我认为您不能在 C/C++/ObjC (Xcode/CDT) 中使用 #region C# (VS) 或 #pragma mark 等任意结构。

I recommend you have a look at PyDev. If you structure your Python code well it will be very useful to have a document outline and code folding. Unfortunately I don't think you can do arbitrary structures like #region C# (VS) or #pragma mark in C/C++/ObjC (Xcode/CDT).

带上头具痛哭 2024-12-25 02:59:34

为了防止 PyCharm 抱怨 PEP 8 违规,请使用

#  region region name here
your code here
#  endregion

To keep PyCharm from complaining about the PEP 8 violation use

#  region region name here
your code here
#  endregion
彩扇题诗 2024-12-25 02:59:34

我知道这是一篇较旧的文章,目前 VS CODE 版本为 1.53.2,可以使用 #region [区域名称] [代码块] #endregion [与您在开头放置的区域的名称相同区域]

#region NameOfRegion
def name().....
#endregion NameOfRegion

区域已撤回
扩展区域

I know it is a older post, currently with VS CODE in version 1.53.2 it is possible to use #region [name of the region] [block of code] #endregion [the same name of the region you put in the beginning of region]

#region NameOfRegion
def name().....
#endregion NameOfRegion

region retracted
expanded region

荒芜了季节 2024-12-25 02:59:34

在 Sublime Text 3 中,您只需键入注释并缩进代码,就好像它位于注释下方一样,然后您可以像 C# 一样折叠它

# Region
    (some code here...)

In sublime text 3 you can simply type a comment and indent your code as if it was under the comment and then you can fold it just like C#

# Region
    (some code here...)
来世叙缘 2024-12-25 02:59:34

是的,您可以采用与我们在 C# 中所做的相同的方式,但请记住这是针对 pycharm IDE
示例

#region Description
...
#endregion

参考此处

Yes you can in same way as we did in c#,but keep in mind this is for pycharm IDE
example

#region Description
...
#endregion

reference here

海夕 2024-12-25 02:59:34

不确定这在其他编辑器中是否有效,但在spyder中

# %%

会启动一个新部分。可用于折叠代码或仅运行代码段。

Not sure if this works in other editors, but in spyder

# %%

starts a new section. Can be used to fold code or run just segments of code.

第七度阳光i 2024-12-25 02:59:34

在 PyCharm 中:

# region ----- description
...
# endregion

在具有 Python 扩展 的 Visual Studio Code 中:

#region ----- description
...
#endregion

In PyCharm:

# region ----- description
...
# endregion

In Visual Studio Code with Python expansion:

#region ----- description
...
#endregion
2024-12-25 02:59:34

不需要任何其他工具,我就用这个作弊:-

true=True
如果为真:

然后使用基本编辑器工具折叠 if 语句。

嵌套它们意味着可以折叠成组的块

without any need for any other tool, I cheat nasty with this :-

true=True
if true:

and then fold the if statement using the basic editor tools.

Nesting them means is is possible to fold up groups of blocks

千と千尋 2024-12-25 02:59:34

只需使用类...不知道为什么这如此困难

class MyNewClass:
    def MyFirstMethod(self):
        print("MyFirstMethod in MyFirstClass")

这将提供您正在寻找的结构。自 Python 2.6 起经过测试和工作,

如果您没有在 Python 中使用过类,您可以像在 c# 中一样调用

x = MyNewClass()
x.MyFirstMethod()

它们

Just use classes ... not sure why this is so difficult

class MyNewClass:
    def MyFirstMethod(self):
        print("MyFirstMethod in MyFirstClass")

This will provide the structure you are looking for. Tested and works since Python 2.6

if you have not used classes in Python you can call them much like you do in c#

x = MyNewClass()
x.MyFirstMethod()

Enjoy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文