1. 程式人生 > >python基礎學習日誌day8-動態導入和斷言

python基礎學習日誌day8-動態導入和斷言

onerror 學習日誌 動態 nbsp error print 變量 alex 重要

一:動態導入importlib

在程序運行的過程中,根據變量或者配置動態的決定導入哪個模塊,可以使用模塊importlib

importlib使用示例

技術分享

技術分享

二:斷言assert

  如果接下來的程序依賴於前面的,而後面程序很重要,不能出錯。可以用assert
,如果檢查不過關就拋出AssertionError

  

# -*- coding:utf-8 -*-
__author__ = shisanjun

import importlib


#__import__(‘lib.aa‘)  這是解釋器自己內部用的,輸出的是lib

aa=importlib.import_module("
lib.aa") print(aa) print(aa.C("alex")) #斷言assert,如果為真,繼續向下面執行,如果不為真拋出AssertionError assert type(aa.C("alex").name) is str print("ddd") #assert可以用下面方法,但是assert更加高大尚 if type(aa.C("alex").name) is str: print("ddd") else: exit()

python基礎學習日誌day8-動態導入和斷言