problem with Descriptors

Peter Otten __peter__ at web.de
Tue Jun 1 02:16:52 EDT 2010


Steven W. Orr wrote:

> I just discovered descriptors but what I want to do isn't working right.

> That's the part that works. I'm trying to use the above construct to
> implement a new kind of dict().
> 
> The idea is that I want to create a dict that knows about certain values
> that can have dependencies on other values. If you change a value that is
> a dependency of another value within the dict, then the target value will
> automatically recompute itself. In the example below, the value of the key
> "range" is dependent on the value of the key "stop".
> 
> When I run the code, the function recalc_range seems to be successfully
> saved as a bound method. (The save happens in AddDep) But then when I try
> to invoke the saved bound method, it yells at me that the arg to
> __getitem__ is of the wrong type.
> 
> Does anyone see what I did wrong?

>        self.recalc_f[idx] = jj[2].__get__(BalancedDict, self)

The __get__() method's signature is

__get__(self, obj, type=None)

not the other way round. But the real problem is that you are not heeding 
Kernighan's warning that debugging is twice as hard as writing the code in 
the first place...

Peter



More information about the Python-list mailing list