Search code examples
pythonbus

Python: How do I bus multiple lines of code in a function so that they can be turned off with a single # to change all to a comment?


That's basically it.

I have a function that is called to process text and translate it into something else. The GUI doesn't allow me to copy paste inside the virtual environment, so I added some dev options on the backend to print what is input through the console so I can copy it and paste it into another document on the side.

Anyways, I basically just want to bus multiple print statements so that ....

OK I'm dumb I solved this and just rephrased everything as a single print statement with commas, but can anyone humor me and nonetheless tell me how to group multiple lines of code and be able to essentially turn them off with a switch on the backend?

I fixed it by changing

print(thing_1)
print("\n")
print(thing_2)
print("\n")

to:

print(thing_1, "\n", thing_2, "\n")

Solution

  • if True:
        print(thing_1)
        print("\n")
        print(thing_2)
        print("\n")
    

    Change True to False and they're "commented out".

    Use a boolean variable if you want "a switch on the backend".


    If this is purely about printing information, then use logging. Change the logging level between debug, info or warning to have less and less output. There's quite a bit of information about logging in Python; you could start with the logging cookbook.