在 C 语言中,我们编写的代码类似于:
#ifdef DEBUG
printf("Some debug log... This could probably be achieved by python logging.Logger");
/* Do some sanity check code */
assert someCondition
/* More complex sanitycheck */
while(list->next){
assert fooCheck(list)
}
#endif
有方法可以用Python做到这个吗?
编辑:我得到了我的答案,还有更多 :) Paolo、Steven Rumbalski和J Sebastian给了我我正在寻找的信息。感谢das详细的答案,虽然我现在可能不会使用预处理器。
J Sebastian的评论被删除了,因为他在发布评论的答案被删除了,我想。 他说我可以使用Logger中的isEnabledFor()方法来提供一个条件。
感谢大家的回答。这是我的第一个问题。我希望我能接受Paolo或J Sebastian的答案。但由于那些作为评论提供,所以我将接受DAS的答案。
我可能会使用http://nestedinfiniteloops.wordpress.com/2012/01/15/if-debug-python-flavoured/或Logger.isEnabledFor()
DEBUG
并使用简单的 if 语句if DEBUG:...
有什么问题吗? - Steven Rumbalski