异常处理
异常处理:
d = {'a': 2}
try:
d['c']
except (KeyError, IndexError) as e:
print("not good key %s " % e)
except IndexError as e:
print("not good key %s " % e)
except Exception as e: #抓住所有错误, 一般放在最后
pass
else:
print("一切正常")
finally:
print("有没有错都执行")
自定义异常:
raise
动态加载模块
import importlib
aa = importlib.import_module("lib.aa")
print(aa.C().name)
断言
class ClassABC(object):
def __init__(self, name):
self.name = name
obj = ClassABC('hh')
assert type(obj.name) is str