乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python中if __name__ =='__main__':作用

       小豬窩969 2015-02-02
      #hello.py
      def sayHello():
      str
      ="hello"
      print(str);

      if __name__ == "__main__":
      print ('This is main of module "hello.py"')
      sayHello()

      python作為一種腳本語(yǔ)言,我們用python寫(xiě)的各個(gè)module都可以包含以上那么一個(gè)累死c中的main函數(shù),只不過(guò)python中的這種__main__與c中有一些區(qū)別,主要體現(xiàn)在:

      1、當(dāng)單獨(dú)執(zhí)行該module時(shí),比如單獨(dú)執(zhí)行以上hello.py: python hello.py,則輸出

      This is main of module "hello.py"
      hello

      可以理解為"if __name__=="__main__":" 這一句與c中的main()函數(shù)所表述的是一致的,即作為入口;

      2、當(dāng)該module被其它module 引入使用時(shí),其中的"if __name__=="__main__":"所表示的Block不會(huì)被執(zhí)行,這是因?yàn)榇藭r(shí)module被其它module引用時(shí),其__name__的值將發(fā)生變化,__name__的值將會(huì)是module的名字。比如在python shell中import hello后,查看hello.__name__:

      >>> import hello
      >>> hello.__name__
      'hello'
      >>>

      3、因此,在python中,當(dāng)一個(gè)module作為整體被執(zhí)行時(shí),moduel.__name__的值將是"__main__";而當(dāng)一個(gè)module被其它module引用時(shí),module.__name__將是module自己的名字,當(dāng)然一個(gè)module被其它module引用時(shí),其本身并不需要一個(gè)可執(zhí)行的入口main了。可以說(shuō)python中的這種用法很靈活啊。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多