英文:
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?
问题
这基本就是这样。
我有一个函数,用于处理文本并将其翻译成其他内容。GUI 不允许我在虚拟环境中复制粘贴,所以我在后端添加了一些开发选项,通过控制台打印输入的内容,这样我就可以复制它并粘贴到另一个文档中。
总之,我基本上只想要合并多个打印语句,以便...
好吧,我解决了这个问题,只需将所有内容重新表述为一个带有逗号的单个打印语句,但是否有人能幽默一下,告诉我如何分组多行代码并能够在后端使用开关来关闭它们?
我通过将以下内容更改为:
print(thing_1, "\n", thing_2, "\n")
来修复它。
英文:
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")
答案1
得分: 1
if False:
print(thing_1)
print("\n")
print(thing_2)
print("\n")
将 True
更改为 False
,它们被“注释掉”。
如果这纯粹是关于打印信息的话,那么请使用日志记录。在调试、信息或警告之间更改日志记录级别以减少输出。关于Python日志记录有很多信息;您可以从日志记录指南开始了解。
<details>
<summary>英文:</summary>
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](https://docs.python.org/3/howto/logging-cookbook.html#logging-cookbook).
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论