Force exception on attribute write access only one object

Peter Otten __peter__ at web.de
Sat Jan 10 02:53:47 EST 2009


Thomas Guettler wrote:

> Peter Otten schrieb:
>> 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.

>> 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"

> your idea was good, but it does not work with Django ORM Models:
> 
> Traceback (most recent call last):
>   File "/localhome/modw/django/core/handlers/base.py", line 87, in
>   get_response
>     response = callback(request, *callback_args, **callback_kwargs)
>   File "/localhome/modw/foo/views/filter.py", line 473, in add
>     return edit(request, 'add')
>   File "/localhome/modw/foo/views/filter.py", line 493, in edit
>     filter=form.save()
>   File "/localhome/modw/foo/views/filter.py", line 457, in save
>     action=form.save()
>   File "/localhome/modw/django/forms/models.py", line 315, in save
>     if self.instance.pk is None:
>   File "/localhome/modw/django/db/models/base.py", line 292, in
>   _get_pk_val
>     return getattr(self, meta.pk.attname)
> AttributeError: 'MyAction' object has no attribute 'filter_action_ptr_id'

I can't tell what's happening from the traceback alone.
Is "filter_action_ptr_id"" your actual "foo" and "MyAction" your "B"?
Maybe the relevant setup code would help...

Peter




More information about the Python-list mailing list