[CentralOH] Dynamic Access to Class Properties

Mark Erbaugh mark at microenh.com
Wed Jun 18 04:08:20 CEST 2008

On Tue, 2008-06-17 at 21:24 -0400, Mark Erbaugh wrote:
> I suspect I've gone about this all wrong, but anyways...
> I have developed a class that encapsulates access to a moderately
> complex database object.  Once data has been retrieved from the database
> the various fields are accessed using properties.  There are about 70
> such properties. Once the fields have been manipulated, the data can be
> written back to the database using a class method.
> On the GUI end, I developed a GUI where each user edit field knows the
> name of the property which contains the field it represents. A method of
> the form containing the user edit fields can tell all of the edit fields
> to load the data from or to save the data to the data instance.
> For example the facility name is accessed as the property facility_name.
> The edit field for facility name has a string data member with the value
> 'facility_name'.
> Is there a way, other than exec'ing some code to convert the string
> 'facility_name' into the data instance property facility_name?
> Another thought.  Would it be possible for the edit field to store the
> actual property, rather than the value it contains?  For example, say
> the edit field has a data member called prop.  Is there a way that I
> could make it so when the data member could do value = prop to retrieve
> and prop = value to set the property value? Is this currying?  FWIW, I'm
> using Python 2.4, I know there were some changes regarding this in
> Python 2.5.

I did a little more research. I guess it's not really currying.
Anyways, here's a way I found to translate a string into the descriptor
instance for the property

x = main class instance
'prop' = property name

d = type(x).__dict__['prop']

returns the descriptor instance

you can then call d.__get__(x) to get the value
and d.__set__(x, value) to set the value

Any other suggestions?


More information about the CentralOH mailing list