使用字典初始化 class 的实例
Initializing a instance of a class with dictionary
尝试学习如何使用他们的会议示例在 Google App Engine 上部署服务器应用程序。
在示例代码中,我看到以下行
# create Conference & return (modified) ConferenceForm
Conference(**data).put()
在哪里
1. 会议是 class 派生自 ndb.Model 的成员列表(请参阅问题末尾的定义)
- data是一个字典,其中一些键与会议成员的名字相同
所以,基本上看起来上面的代码正在创建一个 Conference class 的实例,其值设置为与字典数据中的键关联的值。
这是一个通用的 Python 代码(即创建一个 class 的实例,其值是用字典中的值初始化的)还是 ndb.Model 初始化函数中有什么东西使用 gettattr/setattr 完成上述操作?
会议定义class
class Conference(ndb.Model):
"""Conference -- Conference object"""
name = ndb.StringProperty(required=True)
description = ndb.StringProperty()
organizerUserId = ndb.StringProperty()
topics = ndb.StringProperty(repeated=True)
city = ndb.StringProperty()
startDate = ndb.DateProperty()
month = ndb.IntegerProperty()
endDate = ndb.DateProperty()
这是 NDB 特定的实现(尽管其他 Python classes 当然也可以这样做)。如果您浏览到 NDB 模型 class 代码,您将看到:
def __init__(*args, **kwds):
...
self._set_attributes(kwds)
'_set_attributes' 设置这些值(如果它们有效)。
请注意,某些键用于其他目的,例如:key、id(以及更多)。
尝试学习如何使用他们的会议示例在 Google App Engine 上部署服务器应用程序。
在示例代码中,我看到以下行
# create Conference & return (modified) ConferenceForm
Conference(**data).put()
在哪里 1. 会议是 class 派生自 ndb.Model 的成员列表(请参阅问题末尾的定义)
- data是一个字典,其中一些键与会议成员的名字相同
所以,基本上看起来上面的代码正在创建一个 Conference class 的实例,其值设置为与字典数据中的键关联的值。
这是一个通用的 Python 代码(即创建一个 class 的实例,其值是用字典中的值初始化的)还是 ndb.Model 初始化函数中有什么东西使用 gettattr/setattr 完成上述操作?
会议定义class
class Conference(ndb.Model):
"""Conference -- Conference object"""
name = ndb.StringProperty(required=True)
description = ndb.StringProperty()
organizerUserId = ndb.StringProperty()
topics = ndb.StringProperty(repeated=True)
city = ndb.StringProperty()
startDate = ndb.DateProperty()
month = ndb.IntegerProperty()
endDate = ndb.DateProperty()
这是 NDB 特定的实现(尽管其他 Python classes 当然也可以这样做)。如果您浏览到 NDB 模型 class 代码,您将看到:
def __init__(*args, **kwds):
...
self._set_attributes(kwds)
'_set_attributes' 设置这些值(如果它们有效)。
请注意,某些键用于其他目的,例如:key、id(以及更多)。