繁体   English   中英

如何在模块对象上访问dir()Python属性?

[英]How to access dir() Python attributes on a module object?

我是Python的新手。 我在一家使用Python 2.7的公司工作。

我导入一个具有函数字段和SQLAlchemy类的模块对象。 我需要对课程做点什么。 我以为我可以通过在导入的模块对象上调用dir()来访问类,然后循环遍历从dir()返回的值,然后测试每个值以查看它是否是类。 我认为这会奏效:

def get_legacy_data_and_serialize(): 
    for m in dir(all_models):
        if inspect.isclass(getattr(all_models, m)):
            print(all_models.m)

但我得到:

AttributeError: 'module' object has no attribute 'm'

当我调用dirs()时,我会收到这样的项目:

Base
Client
ClientDetail
Comment
Common
Contact
File
FormData
Ghost
Identified
LabSet
Message
Sender
Subscription
Todo
TodoList
User
UserDetail
__all__
__builtins__
__doc__
__file__
__name__
__package__
__path__
_create_practice
add
add_templates
add_todo
and_
app

大写名称是我追求的名字。 我如何访问它们?

改变这个:

all_models.m

对此:

getattr(all_models, m)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM