Mar*_*coB 3 python google-app-engine entity properties google-cloud-datastore
如果我有一个从db.Expando派生的实体,我可以通过为新属性赋值来编写Dynamic属性,例如在这个例子中为"y":
class MyEntity(db.Expando):
x = db.IntegerProperty()
my_entity = MyEntity(x=1)
my_entity.y = 2
Run Code Online (Sandbox Code Playgroud)
但是假设我在变量中有动态属性的名称...我如何(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]
Run Code Online (Sandbox Code Playgroud)
谢谢...
是的你可以.您只需在实体上设置属性:
some_name = 'wee'
setattr(my_entity, some_name, 'value')
print getattr(my_entity, some_name)
my_entity.put()
Run Code Online (Sandbox Code Playgroud)
setattr并且getattr是Python的内置函数,用于在对象上设置/获取具有任意名称的属性.
| 归档时间: |
|
| 查看次数: |
571 次 |
| 最近记录: |