A failed attempt to combine __getattr__ with properties

Sean Ross sross at connectmail.carleton.ca
Wed Feb 5 13:09:46 EST 2003


Hi.
I think you could hack something together that would work using the folowing
recipe as a basis:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768
Good luck, and have fun,
Sean


"David Casti" <david at neosynapse.net> wrote in message
news:8454be6c.0302050650.2d45cb0f at posting.google.com...
> I have a class which is used to stage information before it is loaded
> into a database.  One of the dictionaries in this class manages the
> fields, values, and constraints.  For example, if the dictionary
> contained --
>
> { 'name': { 'value': 'Name', 'maxlen': 10, 'typname': 'varchar' } }
>
> -- then there would be a column in the database named 'name', using
> the varchar datatype, with a maximum length of 10.
>
> I have created a __getattr__ function which creates get_ and set_
> accessor functions on demand.  For example, there is no need to
> explicitly declare get_name and set_name, because __getattr__ will
> automatically create get & set functions to do type coercion, etc.  So
> far, this system has worked out very well.
>
> Now I'm working with an application which involves taking input from a
> web page and updating a table.  If I was stupid, I'd just say
> something like --
>
>     eval ( "self.set_%s( %s )" % ( col, value ) )
>
> -- but I can't trust web input, so 'eval'ing is right out.  I have
> thought of using properties.  So, I have tried --
>
>    name = property( get_name, set_name )
>
> -- in my class definition, but this fails at compile time because
> get_foo is not defined.
>
> Is there a better way to approach this problem?






More information about the Python-list mailing list