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?

huangapple go评论67阅读模式
英文:

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&#39;re &quot;commented out&quot;.

Use a boolean variable if you want &quot;a switch on the backend&quot;.

----

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&#39;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>



huangapple
  • 本文由 发表于 2023年7月24日 18:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定