setattr vs readonly property

Discussion in 'Python' started by james_027, Sep 12, 2007.

  1. james_027

    james_027 Guest

    hi,

    My main purpose for using setattr(object, attr, value) for assign
    values from a dict that has some keys that may not be present on some
    object's attibute, which makes it work for me. My problem is dealing
    with read only attribute like sample_attribute =
    property(f_get=_get_sample_attribute). what approach should I use? Or
    is my first approach In the first place right?

    Thanks
    james
    james_027, Sep 12, 2007
    #1
    1. Advertising

  2. james_027 wrote:

    > hi,
    >
    > My main purpose for using setattr(object, attr, value) for assign
    > values from a dict that has some keys that may not be present on some
    > object's attibute, which makes it work for me. My problem is dealing
    > with read only attribute like sample_attribute =
    > property(f_get=_get_sample_attribute). what approach should I use? Or
    > is my first approach In the first place right?


    Since read-only properties are very concise written as this:


    class Foo(object):
    @property
    def ro_prop(self):
    return "whatever"

    I'd say that's the way to go - using __getattr__ for this will lead to
    rather convoluted code IHMO.

    Diez
    Diez B. Roggisch, Sep 12, 2007
    #2
    1. Advertising

  3. james_027

    David Guest

    > My main purpose for using setattr(object, attr, value) for assign
    > values from a dict that has some keys that may not be present on some
    > object's attibute, which makes it work for me. My problem is dealing
    > with read only attribute like sample_attribute =
    > property(f_get=_get_sample_attribute). what approach should I use? Or
    > is my first approach In the first place right?


    You could try catching the exception thrown when you attempt to assign
    to a read-only attribute. Although you may get consistency issues. ie
    your object doesn't have all the details from the dict, but the
    calling code expects them to be set. Perhaps you should provide
    "setter" methods for the properties also, so that your objects
    internal state gets set correctly.

    eg: in your class declaration:

    def setfoo(self, value):
    # Add code here to check 'value' and set internal 'foo' state correctly
    self._foo = value

    def getfoo(self):
    return self._foo

    foo = property(setfoo, getfoo)
    David, Sep 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    4
    Views:
    18,867
    Jon Maz
    Feb 20, 2004
  2. =?iso-8859-1?q?Joey_Ch=F6mpff?=

    ObjectDataSource and ReadOnly Object Property

    =?iso-8859-1?q?Joey_Ch=F6mpff?=, Jan 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?iso-8859-1?q?Joey_Ch=F6mpff?=
    Jan 13, 2006
  3. =?Utf-8?B?Sm9leSBDaMO2bXBmZg==?=

    ObjectDataSource and ReadOnly Object Property

    =?Utf-8?B?Sm9leSBDaMO2bXBmZg==?=, Jan 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    554
    =?Utf-8?B?Sm9leSBDaMO2bXBmZg==?=
    Jan 14, 2006
  4. John Smith
    Replies:
    5
    Views:
    7,008
    Bob Barrows [MVP]
    Jun 6, 2006
  5. Jonathan Hyatt
    Replies:
    1
    Views:
    1,842
    Jonathan Hyatt
    Jun 8, 2004
Loading...

Share This Page