views:

207

answers:

2

By creating datastore models that inherit from the Expando class I can make my model-entities/instances have dynamic properties. That is great! But what I want is the names of these dynamic properties to be determined at runtime. Is that possible?

For example,

class ExpandoTest (db.Expando):
 prop1 = db.StringProperty()
 prop2 = db.StringProperty()

entity_one = ExpandoTest()
entity_two = ExpandoTest()

# what I do not want
entity_one.prop3 = 'Demo of dynamic property'

# what I want
entity_two.<property_name_as_entered_by_user_at_runtime> = 'This
property name was entered by the user, Great!!'

Is this possible? If yes, how to do this? I have already tried some funny ways to do this but did not succeed :P

Thanks in advance.

A: 

Just found the solution to my own question. It was really simple but as I am a python noob I ended up posting the question that you see above.

For the code sample that I had used, this is what needs to be done:

entity_two.__setattr(some_variable, some_value) #where some_variable is populated by user at runtime :)
Wikidkaka
+2  A: 

Usually, we use the setattr function directly.

setattr( entity_two, 'some_variable', some_value )
S.Lott