国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析

瀏覽:2日期:2022-07-31 17:13:49

本文實(shí)例講述了Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景。分享給大家供大家參考,具體如下:

相關(guān)內(nèi)容: __import__ importlib 動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景

首發(fā)時(shí)間:2018-02-23 16:06

__import__:功能: 是一個(gè)函數(shù),可以在需要的時(shí)候動(dòng)態(tài)導(dǎo)入模塊使用: __import__(模塊名) 但對(duì)于多級(jí)目錄,只會(huì)導(dǎo)入第一級(jí)Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析 目錄結(jié)構(gòu):Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析 Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析

mo1=__import__('des')mo2=__import__('child.child')mo3=__import__('child')print(mo1,mo2,mo3)#mo3與mo2相同#同級(jí)目錄使用模塊對(duì)象來(lái)調(diào)用mo1.B()mo1.fun2()#對(duì)于目錄下的,動(dòng)態(tài)導(dǎo)入只會(huì)導(dǎo)入第一級(jí)目錄mo2.child.A()#雖然沒有具體定義類體,但無(wú)錯(cuò)就是成功mo2.child.fun1()mo3.child.fun1()importlib:介紹: 是一個(gè)模塊,可以進(jìn)行動(dòng)態(tài)導(dǎo)入模塊用法: importlib.import_module('模塊名')

import importlibmo1= importlib.import_module(’des’)mo2= importlib.import_module(’child.child’)print(mo1,mo2)#mo2直接到child.childdes_B= mo1.B()mo1.fun2()mo2.fun1()動(dòng)態(tài)導(dǎo)入模塊的使用場(chǎng)景: 動(dòng)態(tài)切換模塊 使用反射判斷是否有對(duì)應(yīng)類、方法,無(wú)則設(shè)置

import importlibmo3= importlib.import_module(’child’)def func4(): print(' run in func4')if hasattr(mo3,'child1'): print('yes') c=getattr(mo3,'child')else: #沒有則設(shè)置 setattr(mo3,'func4',func4)mo3.func4() 其他。。。

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长子县| 榕江县| 大冶市| 佛山市| 昭苏县| 三都| 历史| 德庆县| 崇左市| 砀山县| 绥德县| 大厂| 延吉市| 松原市| 永福县| 图木舒克市| 历史| 郎溪县| 汉川市| 雷州市| 平和县| 洪泽县| 江陵县| 通榆县| 巴中市| 招远市| 安新县| 新昌县| 繁峙县| 彩票| 湛江市| 固原市| 东安县| 静乐县| 从化市| 秦安县| 阳泉市| 达孜县| 武强县| 承德市| 新晃|