[ZPatterns] AttributeError _v_cache
Steve Spicklemire
[email protected]
Thu, 18 Oct 2001 17:10:05 -0500
Yes.. in Python I'm used to something like:
class Foo
myFoo = None
def getMyFoo(self):
if self.myFoo is None:
self.myFoo = MyFoo()
return self.myFoo
Which seems pretty clear to me!
What's the main advantage of the ComputedAttribute version? Is it just
sytactical (i.e., it doesn't look like a method call, but rather an
instance varible)?
take care, and thanks!
-steve
Steve Alexander wrote:
>
> 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