您的位置:首页技术文章
文章详情页

Python基于类路径字符串获取静态属性

【字号: 日期:2022-08-02 15:37:15浏览:8作者:猪猪

一个python类,其类路径字符串是student.Student

class Student: name = ’admin’ age = 12

通过如下方式就能获取到类的属性及其属性值

import importlib# 类的全路径path = ’student.Student’p,c = path.rsplit(’.’,maxsplit=1)m = importlib.import_module(p)# 类的clscls = getattr(m,c)# print(cls) #<class ’student.Student’>for key in dir(cls): if not key.startswith(’__’): print(key,getattr(cls,key)) # age 12 ; name admin

Python基于类路径字符串获取静态属性

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Python 编程
相关文章: