99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學(xué)院 頭像
蘋(píng)果6袋
6
麥子學(xué)院

Python學(xué)習(xí)之模塊按需加載方法詳解

發(fā)布時(shí)間:2017-08-30 20:02  回復(fù):0  查看:2364   最后回復(fù):2017-08-30 20:02  
有時(shí)候不想一開(kāi)始就把模塊加載進(jìn)來(lái),當(dāng)調(diào)用這個(gè)模塊的函數(shù)時(shí),再加載,這樣有什么方法嗎?本文和大家分享的就是python 中模塊按需加載相關(guān)內(nèi)容,一起來(lái)看看吧,希望對(duì)大家 學(xué)習(xí)python有所幫助。
  具體的模塊按需加載, 可以按下面的例子來(lái)實(shí)現(xiàn):
  # File: builtin-import-example-3.py
   class  LazyImport:
   def  __init__(self, module_name):
  self.module_name = module_name
  self.module =  None
   def  __getattr__(self, name):
   if self.module  is  None:
  self.module = __import__(self.module_name)
   return getattr(self.module, name)
  string = LazyImport("string")
  print(string.ascii_letters)
  運(yùn)行結(jié)果輸出如下:
  === RESTART: D:/work/csdn/python_Game1/example/builtin-import-example-3.py ===
  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  >>>
  在這個(gè)例子里,只當(dāng)調(diào)用函數(shù)LazyImport ()才會(huì)加載相應(yīng)的模塊。
來(lái)源: 大坡3D 軟件開(kāi)發(fā)
您還未登錄,請(qǐng)先登錄

熱門(mén)帖子

最新帖子

?