python做中学(三)条件编译的用法


  C代码中经常使用条件编译,python中该怎么用呢?Python没有像C或C或Java甚至Java一样编译,python文件被“即时”编译,您可以将其视为类似于Basic或Perl的解释语言

只需使用if语句,就可以执行与条件编译等效的操作:我这里写了一个例子,希望对有需求的同学有点帮助:

 1 # python2
 2 
 3 global FLAG
 4 FLAG = True
 5 if FLAG:
 6     def test1():
 7         print('hello world 1')
 8 else:
 9     def test1():
10         printa("test 1 ")
11 def test2():
12     print('hello world 2')
13 
14 if __name__ == '__main__':
15     test1()
16     test2()

  运行结果:

1 ➜  macro git:(master) ✗ py macro_test.py
2 hello world 1
3 hello world 2

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM