I am looking for a way to dynamically create classes with specific properties accessible via typical instance notation.
DynoOne = createClass('DynoOne',props=['A','B'])
d = DynoOne (database='XYZ')
d.A = d.B + 1
DynoTwo = createClass('DynoTwo',props=['A','C','E'])
q = DynoTwo (database='QRS')
q.A = q.C + 2*q.E
Details of how the "props" are actually acquired and modified would be hidden. This also makes it easier to add access to new props as they become available.
I have experimented with techniques such as the following, to get a feel for how python can dynamically produce basic class attributes:
Class factory to produce simple struct-like classes?
My initial reading on python suggests that class properties are one way to handle introducing getter/setter methods for access.
What's not clear is how to dynamically specify property names in the factory constructor method (whether using decorators or explicit property() call)
E.g., using property() . . .
class DynamicClass( someBase ):
def dynamic_getter(self):
# acquire "stuff"
return stuff
def dynamic_setter(self,stuff):
# store "stuff"
pass
dynamic_property_name = property(fget=dynamic_getter,fset=dynamic_setter)
When the class is declared/constructed, I need to create a set per requested prop. E.g., the DynoOne class would have separate property/setter/getter for 'A' and 'B'.
I suspect that a template-based eval() strategy would work, but I am likely missing some more fundamental and effective technique.
Enlightenment and learning opportunities are appreciated :-)