在C我们写像Python的等价物的#ifdef DEBUG
#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做到这一点?
编辑:我得到了我的答案,以及更多:) 保罗,史蒂文伦巴斯基和J塞巴斯蒂安给了我我正在寻找的信息。感谢das的详细答案,尽管我现在可能不会使用预处理器。
J塞巴斯蒂安,他的评论被删除,因为他发表评论的答案,删除了他的答案,我想。他说我可以在记录器中使用isEnabledFor()方法来提供一个条件。
谢谢大家的意见。这是我的第一个问题。我希望我能接受保罗,或者塞巴斯蒂安的答案。但由于这些是作为评论提供的,我会接受达斯的答案。
我可能会使用两种http://nestedinfiniteloops.wordpress.com/2012/01/15/if-debug-python-flavoured/或Logger.isEnabledFor()
http://stackoverflow.com/questions/1593274/how-do-i-check-if-the-python-debug-option-is-set-from-within-a-script and http:// nestedinfiniteloops .wordpress.com/2012/01/15/if-debug-python-flavored/ –
设置全局'DEBUG'并使用简单的if-statment'如果DEBUG:...'有什么问题? –