这个解决方案似乎有助于解决我的问题:Getting attributes of a class
但是,当我按照上面链接的步骤进行操作时,我的项目仅返回 Config
和 fields
。这可能是因为这些类继承自 Pydantic 的 BaseModel
。
我要找的是一个班级,比如说:
class GameStatistics(BaseModel):
id: UUID
status: str
scheduled: datetime
我应该返回 id
、status
和 scheduled
我的尝试:
for name, cls in inspect.getmembers(module, lambda member: inspect.isclass(member) and member.__module__== module.__name__):
for _name, attrbs in inspect.getmembers(cls, lambda member: not(inspect.isroutine(member))):
if not _name.startswith('_'):
print(_name, attrbs)