python路径找类并获取静态字段

 Python通过路径找类并获取其中大写的静态字段

 

settings.py

class Foo:
    DEBUG = True
    TEST = True

 

xx.py

 1 import importlib
 2 
 3 path = "settings.Foo"
 4 
 5 p,c = path.rsplit('.', maxsplit=1)
 6 m = importlib.import_module(p)
 7 cls = getattr(m, c)
 8 
 9 # 如果找到这个类?
10 for key in dir(cls):
11     if key.isupper():
12         print(key, getattr(cls, key))

 

posted @ 2019-01-20 19:03  A╰无忧╮✨  阅读(194)  评论(0编辑  收藏  举报