python路径找类并获取静态字段
Python通过路径找类并获取其中大写的静态字段
settings.py
class Foo:
DEBUG = True
TEST = True
xx.py
import importlib
path = "settings.Foo"
p,c = path.rsplit('.', maxsplit=1)
m = importlib.import_module(p)
cls = getattr(m, c)
# 如果找到这个类?
for key in dir(cls):
if key.isupper():
print(key, getattr(cls, key))