setattr vs readonly property

Diez B. Roggisch deets at nospam.web.de
Wed Sep 12 05:16:52 EDT 2007


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



More information about the Python-list mailing list