[CentralOH] Dynamic Access to Class Properties

Hickey,Thom hickey at oclc.org
Wed Jun 18 16:29:28 CEST 2008

I wouldn't trust anything coming in from a GUI, even if it seemed under
my control.

I'd make the method names distinctive (e.g. some sort of prefix). That
way you can check and make sure something isn't tricking you into
calling a method you don't want exposed.


-----Original Message-----
From: centraloh-bounces at python.org [mailto:centraloh-bounces at python.org]
On Behalf Of Mark Erbaugh
Sent: Tuesday, June 17, 2008 10:08 PM
To: centraloh at python.org
Subject: Re: [CentralOH] Dynamic Access to Class Properties

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
> the various fields are accessed using properties.  There are about 70
> such properties. Once the fields have been manipulated, the data can
> 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
> the form containing the user edit fields can tell all of the edit
> to load the data from or to save the data to the data instance.
> For example the facility name is accessed as the property
> The edit field for facility name has a string data member with the
> '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
> and prop = value to set the property value? Is this currying?  FWIW,
> 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?


CentralOH mailing list
CentralOH at python.org

More information about the CentralOH mailing list