I know python does not formally support any built in regional code folding syntax. I do know that a few syntaxes exist out in the wild with conventions tied to specific editors and particular comment pairs.
I know of the python source code folding syntax recognized by VS Code which uses the #region
keyword
#region MY_CODE_REGION
...
#endregion
I came across another I don't recognize taken from some source over at
https://github.com/gitpython-developers/GitPython/blob/master/git/objects/tree.py#L96-L104
which used the following syntax
#{ MY_CODE_REGION
#} END MY_CODE_REGION
What other editor syntaxes exist for python? Is there a pending PEP open on the subject?
It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block.
For example, select a block of code for which you want to create a folding region, press Ctrl+Alt+T , select either <editor fold> or region... endregion comments depending on the style you need, and name the created custom region.
Folding By Region VS Code allows you to specify arbitrary blocks that should be folded. These are called "regions". To create one of these, use a comment with \\#region at the start of the block and \\#endregion at the end.
The number of foldable regions is limited to a maximum of 5000.
I think #%%
is another option for the syntax you are looking for. This is from the Spyder editor as the method for separating sections of code (and it is listed there as the "standard cell divider" syntax)
https://docs.spyder-ide.org/3/editor.html#defining-code-cells
there are also ways to convert this to and from a jupyter notebook
https://stackoverflow.com/a/55920098/13665895
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With