I am new to Python. I am working at a company that uses Python 2.7.
I import a module object that has functions fields and SQLAlchemy classes. 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. 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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.