【发布时间】:2011-03-04 10:16:35
【问题描述】:
如果我有一个从 db.Expando 派生的实体,我可以通过将值分配给新属性来编写动态属性,例如本例中的“y”:
class MyEntity(db.Expando):
x = db.IntegerProperty()
my_entity = MyEntity(x=1)
my_entity.y = 2
但是假设我在变量中有动态属性的名称...我如何 (1) 读取和写入它,以及 (2) 检查动态变量是否存在于实体的实例中?例如
class MyEntity(db.Expando):
x = db.IntegerProperty()
my_entity = MyEntity(x=1)
# choose a var name:
var_name = "z"
# assign a value to the Dynamic variable whose name is in var_name:
my_entity.property_by_name[var_name] = 2
# also, check if such a property esists
if my_entity.property_exists(var_name):
# read the value of the Dynamic property whose name is in var_name
print my_entity.property_by_name[var_name]
谢谢...
【问题讨论】:
标签: python google-app-engine properties entity google-cloud-datastore