[ZPatterns] AttributeError _v_cache

Steve Alexander steve@cat-box.net
Thu, 18 Oct 2001 17:55:15 +0100


Steve Spicklemire wrote:

> 
> Hi Joachim,
> 
>     Hmm... this is in my Rack.py for TransactionAgents-0.4
> 
>     # Caching
> 
>     def _v_cache(self):
>         self._v_Keeper
>         l = self._v_cache = {}
>         return l
> 
>     _v_cache = ComputedAttribute(_v_cache)
> 
>     __per_transaction_cache_attrs__ = ('_v_cache',)
> 
> 
> 
> I can't say I understand the code, but I probably should. (Hint: if you 
> understand this deeply.. pitch in! ;-> ) However it *looks* like 
> _v_cache is actually defined *twice*. Once as a method of the class, and 
> then as an attribute that is a "ComputedAttribute" wrapped version of 
> the original method. In any case, the attribute is clearly defined in 
> the class, so I can only guess that your instance is not using this 
> class definition. What version of ZPatterns are you using?


Here's what is happening:

In python, a class gets defined by the statements within the class 
definition being interpreted, top to bottom.

First, the name _v_cache in the class's __dict__ is bound to the method 
defined above.

Then _v_cache is bound instead to the old value of _v_cache (that 
method), wrapped as a ComputedAttribute.

This is the final value of _v_cache in the class.

However, when you ask for _v_cache on an object of that class, the 
method gets called. It binds _v_cache in the object to {}. This binding 
in the object overrides the binding in the class.

At a transaction boundary, all per-transaction attrs get deleted, so we 
get back to the previous situation; as there is no _v_cache in the 
object, we get the definition from the class.

--
Steve Alexander
Software Engineer
Cat-Box limited