Commenting Python Code

I’ve noticed a certain behavior there must be a known solution to, wanted to hear how others handle this.

Sometimes if you comment out python lines of code with triple quotes, the code inside the triple quotes will render.

I don’t see the same behavior with using the “#” symbol, code inside that never renders.

For example, if I comment out a line of code:
"""print("Hello!")"""

This shows up in the streamlit app. If I comment it out like this:
# print("Hello!")

It does not show up.

This behaviour is intentional and called magic commands:

https://docs.streamlit.io/library/api-reference/write-magic/magic

@Franky1 is exactly correct.

I myself like to use triple-quotes to comment out blocks of code while I’m iterating on my app, so I typically do this:

_ = """
code to comment out
more code
"""

Assigning it to a variable means that it won’t magically be printed
Assigning it to _ (a standard throw-away variable) means that my auto-linters won’t complain about a variable that I’m not using :slight_smile:

3 Likes

Thank you both, super helpful.

@blackary thank you for the tip, this is a great idea!