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


-- 
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

Reply via email to