您将如何等效于Python中的预处理程序指令? [英] How would you do the equivalent of preprocessor directives in Python?

查看:61
本文介绍了您将如何等效于Python中的预处理程序指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Python中执行以下预处理程序指令?

  #if DEBUG 

<做一些代码>

#else

<进行其他一些代码>

#endif


解决方案

__ debug __ ,这是编译器进行预处理的特殊值。

 如果__debug__:
打印如果打印出来,说明您不是在运行python -O。
else:
打印如果打印,说明您正在运行python -O!

__ debug __ 将被常量0取代或由编译器设置为1,然后优化器将在解释源之前删除所有如果0:行。

Is there a way to do the following preprocessor directives in Python?

#if DEBUG

< do some code >

#else

< do some other code >

#endif

解决方案

There's __debug__, which is a special value that the compiler does preprocess.

if __debug__:
  print "If this prints, you're not running python -O."
else:
  print "If this prints, you are running python -O!"

__debug__ will be replaced with a constant 0 or 1 by the compiler, and the optimizer will remove any if 0: lines before your source is interpreted.

这篇关于您将如何等效于Python中的预处理程序指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆