[ZPatterns] AttributeError _v_cache
Steve Spicklemire
[email protected]
Thu, 18 Oct 2001 15:11:24 -0500
Interesting! Thanks Steve. In snooping through the code, I see this is
somewhat of an idiom (The Keeper class in Transactions pulls the same
trick). It's not at all obvious when you first glance at it though!
Basically it's a way of effectively calling a class based method the
first time an attribute it accessed (which has the side effect of
setting a value for the attribute in the instance), but further
references to the same attribute get the "simple" attribute from the
instance. Have I got that right?
thanks!
-steve
Steve Alexander wrote:
>
> 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
>
> _______________________________________________
> ZPatterns mailing list
> [email protected]
> http://www.eby-sarna.com/mailman/listinfo/zpatterns