If I have an object derived from db.Expando, I can write the Dynamic property by simply assigning a value to the new property, for example. "y" in this example:
class MyEntity(db.Expando): x = db.IntegerProperty() my_entity = MyEntity(x=1) my_entity.y = 2
But suppose I have a dynamic property name in a variable ... how can I (1) read and write to it, and (2) check if a dynamic variable exists in an entity instance? eg.
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]
Thank...
Yes, you can. You just need to set the attribute in the entity:
some_name = 'wee' setattr(my_entity, some_name, 'value') print getattr(my_entity, some_name) my_entity.put()
setattr getattr Python, / .
setattr
getattr
Source: https://habr.com/ru/post/1750279/More articles:How to save an object in fooSettings.settings file in Visual Studio? - c #C # Active Directory through WMI - c #JAVA - strange problem (maybe a thread issue) with JTable & Model - javaWhich device uses JTS LineString.getLength () - javaSync in Robotium - androidWhy does the behavior of Response.Write change in this scenario? - c #simple question: the difficulty of declaring and using a variable - asp.net-mvcSimulation software for designing a network serialization protocol - designcomparing dates in C # with zero in mind - c #Where to host the root web server? - linuxAll Articles