Dividing Python module into multiple regions

Question:

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?

Asked By: gsagrawal

||

Answers:

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

Answered By: Krumelur

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

Answered By: CSJ

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

Answered By: Perk

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

Answered By: Darien Pardinas

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...)
Answered By: Zeus3101

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

Answered By: Muhammad Younus

To keep PyCharm from complaining about the PEP 8 violation use

#  region region name here
your code here
#  endregion
Answered By: Peter Hedlund

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...
Answered By: Tomasz Chudzik

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

Answered By: ElAgus

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

Answered By: Ray Lee

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.

Answered By: Jeremy Matt

In PyCharm:

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

In Visual Studio Code with Python expansion:

#region ----- description
...
#endregion
Answered By: BouRHooD
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.