Dividing Python module into multiple regions

PythonFormatting

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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#.

Solution 3 - Python

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).

Solution 4 - Python

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...

Solution 5 - Python

To keep PyCharm from complaining about the PEP 8 violation use

#  region region name here
your code here
#  endregion

Solution 6 - Python

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...)

Solution 7 - Python

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

Solution 8 - Python

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

Solution 9 - Python

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

Solution 10 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongsagrawalView Question on Stackoverflow
Solution 1 - PythonCSJView Answer on Stackoverflow
Solution 2 - PythonDarien PardinasView Answer on Stackoverflow
Solution 3 - PythonKrumelurView Answer on Stackoverflow
Solution 4 - PythonTomasz ChudzikView Answer on Stackoverflow
Solution 5 - PythonPeter HedlundView Answer on Stackoverflow
Solution 6 - PythonZeus3101View Answer on Stackoverflow
Solution 7 - PythonMuhammad YounusView Answer on Stackoverflow
Solution 8 - PythonElAgusView Answer on Stackoverflow
Solution 9 - PythonRay LeeView Answer on Stackoverflow
Solution 10 - PythonPerkView Answer on Stackoverflow