Force exception on attribute write access only one object

Peter Otten __peter__ at web.de
Thu Jan 8 06:29:19 EST 2009


Thomas Guettler wrote:

> for debugging I want to raise an exception if an attribute is
> changed on an object. Since it is only for debugging I don't want
> to change the integer attribute to a property.

Why?
 
> This should raise an exception:
> 
> myobj.foo=1
> 
> Background:
> Somewhere this value gets changed. But I don't now where.

If you change your mind:

class A(object):
    def __init__(self): 
        self.foo = 42

a = A()
b = A()

class B(A):
    @property
    def foo(self):
        return self.__dict__["foo"]

b.__class__ = B

a.foo = "whatever"
print b.foo
b.foo = "whatever"

Peter



More information about the Python-list mailing list