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?
print(getattr(all_models, m))instead ofprint(all_models.m)