Hi Jason, On 28 Okt., 19:27, Jason Grout <jason-s...@creativetrax.com> wrote: > > * A property would be called over and over again and would do the > > same computation over and over again. > > * A cached method would be called over and over again, but would do > > the computation only once. > > * A lazy attribute would be called only once and thus also do the > > computation only once. > > "Aha!" I thought, "you should be using *cached* properties."
No. Even if it would work, I am sure that it would be slower than a lazy attribute. If I understand correctly, using the @property decorator means that some function "property" is applied to an unbound method property_test. Let the result be P=property(property_test). The object P has a __get__ method. Whenever you do b.property_test in your example, then P.__get__(b) is called, which then calls the original method. Even if the original method was a cached method, the overhead from calling P.__get__(b) is too much, for the application I have in mind. Compare with the @lazy_attribute decorator: lazy_attribute(lazy_test) is some object L. Again, L has a __get__ method. However, L.__get__(b) overwrites the lazy attribute by a *usual* attribute. So, calling b.lazy_test a second time, it really is nothing more than an attribute lookup - no __get__ involved anymore! > TypeError: 'CachedMethod' object is not callable > > I'm not exactly sure why I got this error. Would it be easy for someone > to enlighten me? Yes. The @cached_method decorator applied to an unbound method results in a CachedMethod object. The CachedMethod object has a __get__ method, but no __call__ method. That __get__ method of a cached_method does not return a CachedMethod object, but a CachedMethodCaller object. The CachedMethodCaller has a __call__ method. Here is an example: sage: P.<x,y> = QQ[] sage: I = P*[x,y] sage: type(I.__class__.groebner_basis) <class 'sage.misc.cachefunc.CachedMethodCaller'> sage: type(I.groebner_basis) <class 'sage.misc.cachefunc.CachedMethodCaller'> However, if you prepend the @cached_method decorator by an @property decorator, then @property you essentially have property(cached_method(some_unbound_method)). Thus, the function "property" receives a CachedMethod object, not a CachedMethodCaller instance. It is not callable, thus the error. Cheers, Simon -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org