Я хотел бы иметь возможность получить класс из класса в версии Python для Google App Engine. Как получить класс со строкой, содержащей вид класса?
player_props = Player.properties() country_props = Country.properties() model_list = ['Player', 'Country'] for m in model_list: #foo = theClass props = foo.properties()
Вы можете использовать функцию class_for_kind("Kind") в пакете google.appengine.ext.db. Классы вашей модели должны быть импортированы, чтобы это работало.
class_for_kind("Kind")
google.appengine.ext.db
Пока вы знаете модуль, из которого были загружены классы, вы можете получить классы из модуля.
import models model_list = ['Player', 'Country'] for m in model_list: klass = getattr(models, m) props = klass.properties()
Хорошо, если вы импортировали классы моделей в свой модуль, вы можете использовать функцию globals() :
globals()
for m in model_list: klass = globals()[m] props = klass.properties()