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?
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?
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).
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
#
in both instances –
Diplomate #
. –
Maladminister 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#.
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...
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).
To keep PyCharm from complaining about the PEP 8 violation use
# region region name here
your code here
# 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 NameOfRegion
def name().....
#endregion NameOfRegion
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...)
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
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.
In PyCharm:
# region ----- description
...
# endregion
In Visual Studio Code with Python expansion:
#region ----- description
...
#endregion
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
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
© 2022 - 2024 — McMap. All rights reserved.