properties access by name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?KOI8-R?B?7cnU0Q==?=

    properties access by name

    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_s et = ['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_s et[0]] = 'bbb'

    now I can write:
    f.__dict__['_Film__' + properties_to_s et[0]] = 'bbb'

    but I want to set my property through the provided setter. How can I
    do this?

    P.S. Sorry for my english
  • Rob Williscroft

    #2
    Re: properties access by name

    =?KOI8-R?B?7cnU0Q==?= wrote in news:f1a77a69-2997-4f53-9a46-
    f8bd90d8a67a@m3 g2000hsc.google groups.com in comp.lang.pytho n:
    >
    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_s et = ['title']
    f = Film('aaa')
    Ther is a builtin `setattr` to do this:

    #http://www.python.org/doc/2.5.2/lib/built-in-funcs.html

    setattr( f, "title", "bbb" )

    Rob.
    --

    Comment

    Working...