Search code examples
pycharmcommentscode-folding

about auto -code folding blocks of comments in PyCharm


I'm new to PyCharm tool and trying to figure out the rule about auto-generated code-folding blocks of comments in PyCharm (2020.2.1, Community Edition Build PC#202.6948.78).

Screenshot of my code: How can I show the code blocker in (A) instead of (B)?

In the screenshot, the code blocker appears in (B) which does not help organizing code view. I'd like it to appear in (A).

For this, I tried:

  1. changing number of "#" in different areas, but failed.
  2. added another line of numerous "#"s above line 11: code folding appeared between line 10 and 11
  3. chose line 12 and 13 and pressed Ctrl+".": This worked, but doing this every time would be bothersome.

Would anyone please provide me with some rule about when the comment folding starts and ends so that I simply follow the rule in my coding and set the default code-folding setting to reduce the visible part of my source code?

Thank you in advance.


Solution

  • You have different options:

    1. Just use #%% or # In[] for dividing python code into different sections;
    2. Use the following code to make you code folding and show necessary content introduction:
    #<editor-fold desc="The content to show">
    print('Hello World')
    #</editor-fold>
    

    For folding code, you could use Code - Folding - Collapse All. You could also use shortcut which is showing in the menu which is Command Shift - for macOS.