[英]How to access dir() Python attributes on a module object?
I am new to Python. 我是Python的新手。 I am working at a company that uses Python 2.7. 我在一家使用Python 2.7的公司工作。
I import a module object that has functions fields and SQLAlchemy classes. 我导入一个具有函数字段和SQLAlchemy类的模块对象。 I need to do something with the classes. 我需要对课程做点什么。 I thought I could get access to the classes by calling dir()
on the imported module object, and then looping over the values returned from dir()
, and then testing each value to see if it is a class. 我以为我可以通过在导入的模块对象上调用dir()
来访问类,然后循环遍历从dir()
返回的值,然后测试每个值以查看它是否是类。 I thought this would work: 我认为这会奏效:
def get_legacy_data_and_serialize():
for m in dir(all_models):
if inspect.isclass(getattr(all_models, m)):
print(all_models.m)
but I get: 但我得到:
AttributeError: 'module' object has no attribute 'm'
When I call dirs() I get back items like this: 当我调用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
The upper-case names are the names I'm after. 大写名称是我追求的名字。 How do I access them? 我如何访问它们?
Change this: 改变这个:
all_models.m
To this: 对此:
getattr(all_models, m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.