On 05/02/2012 03:22 PM, Mike Hansen wrote:
On Wed, May 2, 2012 at 12:08 AM, P Purkayastha<[email protected]> wrote:
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.
Like Simon mentioned, you can add getattr and setattr support:
from sage.misc.all import lazy_attribute
cdef class A:
cdef public object __dict__
def __init__(self):
self.__dict__ = {}
def __getattr__(self, attr):
try:
return self.__dict__[attr]
except KeyError:
raise AttributeError
def __setattr__(self, attr, value):
self.__dict__[attr] = value
@lazy_attribute
def a(self):
# moderate amount of computation gives the answer 42
return 42
--Mike
Oh! Thanks a lot! This does work!
I thought Simon was explaining the reason why it didn't work. I am just
vaguely familiar with setattr and getattr, so I didn't think about
defining them in my own class.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org