Python---根据字符串导入包(importlib)
生活随笔
收集整理的这篇文章主要介绍了
Python---根据字符串导入包(importlib)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
import importlib
o = importlib.import_module("xx.oo") # o 为 <module 'xx.oo' from 'D:\\locker\\importlib_demo\\xx\\oo.py'>
print(o.NAME)p = o.Person("cmb")
p.dream()
import importlib # 根据字符串导入模块 # 通常用来导入包下面的模块 o = importlib.import_module("xx.oo") s2 = "Person"# 由字符串找函数、方法、类, 利用反射 the_var = getattr(o, "NAME") print(the_var)the_class = getattr(o, s2) p2 = the_class("小和牛") p2.dream()print(the_class) # <class 'xx.oo.Person'> print(o) # <module 'xx.oo' from 'D:\\locker\\importlib_demo\\xx\\oo.py'> print(o.NAME)p = o.Person("cmb") p.dream()
转载于:https://www.cnblogs.com/Paul-watermelon/articles/10828636.html
总结
以上是生活随笔为你收集整理的Python---根据字符串导入包(importlib)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 美军FMTV系列中型战术军车有多先进?
- 下一篇: Python 3.X 练习集100题 0