properties access by name

Chris Rebert clp at rebertia.com
Fri Oct 17 13:11:20 EDT 2008


Since rwproperty appears to use descriptors just like regular
property(), you'd do it the same way as for any normal attribute,
namely:

#for reading
print foo.y
#is the same as
print getattr(foo, "y")

#for writing
foo.x = 1
#is the same as
setattr(foo, "x", 1)


Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

On Fri, Oct 17, 2008 at 9:36 AM, Митя <netimen at gmail.com> wrote:
> I use rwproperty (http://pypi.python.org/pypi/rwproperty/1.0) and so I
> have properties in my class. Also I have a list of names of properties
> wich I am to set. How can I access my properties by name in such way
> that when I want to set a property, setter will be called, and and
> when I want to read it - getter?
>
> I have something like this:
>
> class Film(object):
>    def __init__(self, title):
>        self.__title = title
>
>    @getproperty
>    def title(self):
>        return self.__title
>    @setproperty
>    def title(self, value):
>        self.__title = value
>
> properties_to_set = ['title']
> f = Film('aaa')
>
> I d want to have sth like:
> f(properties_to_set[0]) = 'bbb'
>
> If title was a simple variable I could write:
> f.__dict__[properties_to_set[0]] = 'bbb'
>
> now I can write:
> f.__dict__['_Film__' + properties_to_set[0]] = 'bbb'
>
> but I want to set my property through the provided setter. How can I
> do this?
>
> P.S. Sorry for my english
> --
> http://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list