On 10/28/11 9:28 AM, Simon King wrote:
Hi Maarten,
On 28 Okt., 15:32, Maarten Derickx<m.derickx.stud...@gmail.com>
wrote:
Well, it is supported if you make the attribute a property. Since most of
those things are already functions it would be very easy
...
http://docs.python.org/library/functions.html#property
Thank you, I didn't know "properties" before. But I think it is not
the right thing:
sage: class Bar(object):
.....: @property
.....: def property_test(self):
.....: print "calling test"
.....: return 5
.....: @lazy_attribute
.....: def lazy_test(self):
.....: print "calling test"
.....: return 7
.....:
sage: b = Bar()
sage: b.property_test
calling test
5
sage: b.property_test
calling test
5
sage: b.lazy_test
calling test
7
sage: b.lazy_test
7
Hence,
* 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." But then I
tried it:
sage: class Bar(object):
....: @property
....: @cached_method
....: def property_test(self):
....: print "calling test property"
....: return 5
....: @lazy_attribute
....: def lazy_test(self):
....: print "calling lazy attribute"
....: return 7
....:
sage: b=Bar()
sage: b.property_test
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[snip]<ipython console> in <module>()
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?
Also, it seems that the brilliant people that implemented lazy_attribute
thought deeply about it compared with properties; at least, there is
some discussion of properties in the docstring for lazy_attribute. It
would be great if someone who has thought more deeply about the relative
advantages of each to chime in. My guess is that there are some
inheritance issues where the two approaches differ.
Thanks,
Jason
--
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