C/C++-like 宏 #ifdef 0 注释掉 python 中的代码的方法

C/C++-like macro #ifdef 0 way to comment off code in python

在C/C++中,使用如下宏来注释大块代码很常见。

#ifdef 0
//comment block code
#endif

是否有等效的方法来注释掉 python 中的代码块?

Is there an equivalent way to comment off blocks of code in python?

No. Python没有预处理器,其翻译阶段与from C. Please refer to Python's documentation.

不同

我建议在每个无用的行前简单地附加一些 #- 字符串(并使用一些 version control system like git 来保留旧版本;这样你甚至可能不需要注释掉大代码块,因为它们由您的版本控制系统保存)。

优秀的编辑器(如emacs) have facilities (like its rectangle操作)可以轻松地在多行前面添加一个字符串。

顺便说一句,注释掉(或 #if 0-ing 它)一大段代码是不好的做法(即使在 C 或 C++ 中),它会使您的代码不可读。所以不要那样做!

如果您使用 git,请添加一个小评论,例如

# many obsolete functions like foo and bar 
# have been removed after commit 670aaf569a7cc104e

恕我直言,这比保留和注释掉数百个无用的源代码行要好得多,这就是为什么我认为注释掉很多行代码几乎总是一个错误。