On 05/02/2012 02:56 PM, Simon King wrote:
Hi!
On 2012-05-02, P Purkayastha<ppu...@gmail.com> wrote:
------=_Part_843_14196874.1335939956360
Content-Type: text/plain; charset=ISO-8859-1
Is there a way to use lazy_attribute in cython code?
lazy_attribute requires that the object allows attributes to be set.
Thus, it works if you have a non-cdef class (even if the class is
defined in Cython), but I guess you'd need to implement getattr/setattr
support for a cdef class before being able to use it.
Example:
sage: cython("""
....: from sage.misc.lazy_attribute import lazy_attribute
....: class A:
....: @lazy_attribute
....: def a(self):
....: return "A"
....: cdef class B:
....: @lazy_attribute
....: def b(self):
....: return "B"
....: """)
sage: a = A()
sage: b = B()
sage: a.a
'A'
sage: b.b
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
...
AttributeError: '_home_simon__sage_temp_linux_sqwp_site_5210_tmp_0_' object
attribute 'b' is read-only
Background: When a lazy attribute L is requested first for an object O, then
L is looked up in O.__class__. It has a __get__ method to bind it to O,
and the __get__ method overrides L in O. Hence, we need that
setattr(O,'L',some_value) works (which doesn't, in the example above).
Best regards,
Simon
Thanks a lot for your answer. What I really need is some kind of lazy
evaluation. If there is some way to defer an evaluation in cython code,
then also my objective would be satisfied. Something like this:
cdef class A:
cdef public int a
def __init__(self):
# is there any way to not evaluate this on init?
self.a = self._get_a()
def _get_a(self):
# moderate amount of computation gives the answer 42
return 42
Currently, in my Sage (python) code, I used lazy_attribute to achieve
this. I tested the computation using cython and without lazy attribute
and it speeds up by a factor of about 3 compared to the python code. So,
I am looking for something that can defer the computation so that
instantiating the class is fast.
--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org