将Python模块划分为多个区域
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
看起来 PyCharm 有它,请参见此处: https://www. jetbrains.com/help/pycharm/2016.1/code-folding.html#using_folding_comments
对于 Python 文件,支持以下两种样式。您不应该将它们混合在一个文件中。
或者
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.
or
Visual Studio accepts "region" too
通过 Visual Studio 的 Python 工具,您可以使用:
然后您可以像在 C# 上一样折叠区域。
With Python Tools for Visual Studio you can use:
Then you can fold the regions same way you do on C#.
使用 VS Code,您可以简单地创建这样的区域,甚至为区域添加名称:
然后可以将其折叠为以下内容:
With VS Code you can simply create regions like this and even add names for the regions:
It can be then folded into the following:
我建议您查看 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).
为了防止 PyCharm 抱怨 PEP 8 违规,请使用
To keep PyCharm from complaining about the PEP 8 violation use
我知道这是一篇较旧的文章,目前 VS CODE 版本为 1.53.2,可以使用 #region [区域名称] [代码块] #endregion [与您在开头放置的区域的名称相同区域]
区域已撤回
扩展区域
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 retracted
expanded region
在 Sublime Text 3 中,您只需键入注释并缩进代码,就好像它位于注释下方一样,然后您可以像 C# 一样折叠它
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#
是的,您可以采用与我们在 C# 中所做的相同的方式,但请记住这是针对 pycharm IDE
示例
参考此处
Yes you can in same way as we did in c#,but keep in mind this is for pycharm IDE
example
reference here
不确定这在其他编辑器中是否有效,但在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.
在 PyCharm 中:
在具有 Python 扩展 的 Visual Studio Code 中:
In PyCharm:
In Visual Studio Code with Python expansion:
不需要任何其他工具,我就用这个作弊:-
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
只需使用类...不知道为什么这如此困难
这将提供您正在寻找的结构。自 Python 2.6 起经过测试和工作,
如果您没有在 Python 中使用过类,您可以像在 c# 中一样调用
它们
Just use classes ... not sure why this is so difficult
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#
Enjoy