design question: no new attributes

Arnaud Delobelle arnodel at googlemail.com
Tue Feb 27 02:29:04 EST 2007


On 27 Feb, 06:40, "Alan Isaac" <ais... at american.edu> wrote:
> So my question remains:
> how best to trap any such attempt
> subsequent to object initialization?
> (I am willing to have properties for
> all data attributes, if that helps.)
>

You can define the __setattr__ method in your class as

def __setattr__(self, attr, val):
    if hasattr(self, attr):
        self.__dict__[attr] = val
    else:
        # Tell the user off

but of course the user can set attributes through
instance.__dict__['attrname'] = val

So to really do it you would need to design your own metaclass

--
Arnaud





More information about the Python-list mailing list