【问题标题】:How can I make some code not executable in a Python program? [duplicate]如何使某些代码在 Python 程序中不可执行? [复制]
【发布时间】:2019-02-03 18:54:27
【问题描述】:

我想运行代码的某些部分而不是所有代码,而我不想执行的代码可以被取出,但保留在程序中以备将来使用。

当前代码:

for i in v:

    with open('text.txt'.format(i), 'w') as in_file:
        in_file.write(xxx)

    with open('text.txt'.format(i), 'w') as in_file:
        in_file.write(xxx)

我知道的一种方法是使用“#”将其设为注释,但这非常浪费时间,因为有时我不想执行超过 100 行代码,我应该只使用 # 全部吗?有没有一个好方法可以获取 100 行代码?我什至尝试过 """

预计出处:

for i in v:

    with open('text.txt'.format(i), 'w') as in_file:
        in_file.write(xxx)

  this>> # with open('text.txt'.format(i), 'w') as in_file:
   this >> #     in_file.write(xxx)

【问题讨论】:

  • # 注释掉是最好的答案——通常你会让你的编辑器帮助你处理大块代码。您也可以用""" 包围或缩进并添加if False:,但这些都不是很好。
  • 谢谢先生的回答。我明白了

标签: python-3.x


【解决方案1】:

目前没有用于块 cmets 的方法。

您的编辑器(如果您使用的是 IDE)将可以选择添加和删除多行 cmets。

【讨论】:

  • 我明白了。好的,非常感谢您的回答。
【解决方案2】:

抱歉,由于声誉限制,我无法对此发表评论。

如果您使用的是 IDE,您可以使用一些快捷键来注释每一行。 例如,您可以在 Visual Studio Code 中使用 CTRL+/。它将在所选块的每一行中放置一个单行注释“#”。 IDE 的 Google 快捷键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    相关资源
    最近更新 更多