[ZPatterns] AttributeError _v_cache

Steve Alexander [email protected]
Thu, 18 Oct 2001 22:10:55 +0100


Steve Spicklemire wrote:

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


Sounds good to me.



I think of this as a lazy factory pattern. In a language like Java, 
you'd have a "getter" which creates its value the first time it is called.

   private FooType myFoo = null;

   public FooType getMyFoo() {
     if (myFoo == null) {
       myFoo = new myFoo();
     }
     return myFoo;
   }

(Sorry if the Java isn't quite correct. It's a while since I've used it.)

--
Steve Alexander
Software Engineer
Cat-Box limited