Python region folding syntax
Asked Answered
R

1

15

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?

Rawdin answered 4/9, 2019 at 17:0 Comment(3)
Does this answer your question? Dividing Python module into multiple regionsRosinarosinante
@Rosinarosinante not optimally or exhaustively. Some of the information is there but it's diffused across multiple answers, some of them near if not outright duplicates of one another. The question here isn't so much as how to erect code folding sections but rather what are all the outstanding syntaxes out there today that can accomplish that in a tool specific manner. Most critically though do any pending PEPs cover the subject, because that would suggest a formal syntax is in the works. I was going to answer this one myself when I got some time but never got around to it.Rawdin
You can find syntax for PyCharm here: jetbrains.com/help/pycharm/2016.1/…Barcellona
M
1

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://mcmap.net/q/57379/-how-to-convert-a-python-script-with-cell-delimiters-to-a-jupyter-notebook-duplicate

Meyers answered 23/3, 2022 at 21:47 Comment(2)
Note for VS Code: It supports #%% for marking notebook cells but it seems there is no native support for folding of these cells.Reconcile
Interestingly, Visual Studio supports folding like this.Bartlet

© 2022 - 2024 — McMap. All rights reserved.