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