#19733: Assignment of special methods does not work in Cython
-------------------------------------+-------------------------------------
       Reporter:  jdemeyer           |        Owner:
           Type:  defect             |       Status:  needs_review
       Priority:  blocker            |    Milestone:  sage-6.10
      Component:  cython             |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Jeroen Demeyer     |    Reviewers:
Report Upstream:  N/A                |  Work issues:
         Branch:                     |       Commit:
  u/jdemeyer/assignment_of_special_methods_does_not_work_in_cython|  
fc92afbd3987de9790af923c2f5b97dc3e3da5f8
   Dependencies:                     |     Stopgaps:
-------------------------------------+-------------------------------------
Description changed by jdemeyer:

Old description:

> In Cython, you cannot do
> {{{
> cdef class Foo:
>     __str__ = whatever
> }}}
> and expect it to work.
>
> I don't know if this a Cython or Python bug or feature...
>
> -------
>
> When deriving a `class` (not `cdef class`) this kind of assignment
> suddenly works::
> {{{
> cdef class Base(object):
>     def my_str(self):
>         return "good"
>     __str__ = my_str
>
> class Derived(Base):
>     pass
> }}}
> gives
> {{{
> sage: str(Base())
> '<_home_jdemeyer__sage_temp_tamiyo_8430_tmp__MunIM_spyx_0.Base object
> at 0x7fb54ae544d0>'
> sage: str(Derived())
> 'good'
> }}}
>
> This also explains why the `__len__ = cardinality` in
> `src/sage/rings/finite_rings/finite_field_base.pyx` works. It doesn't
> work on the level of
> `sage.rings.finite_rings.finite_field_base.FiniteField`, but it works at
> the level of the actual finite field classes (which are Python classes).

New description:

 In Cython, you cannot do
 {{{
 cdef class Foo:
     __str__ = whatever
 }}}
 and expect it to work.

 I don't know if this a Cython or Python bug or feature...

 -------

 When deriving a `class` (not `cdef class`) this kind of assignment
 suddenly works::
 {{{
 cdef class Base(object):
     def my_str(self):
         return "good"
     __str__ = my_str

 class Derived(Base):
     pass
 }}}
 gives
 {{{
 sage: str(Base())
 '<_home_jdemeyer__sage_temp_tamiyo_8430_tmp__MunIM_spyx_0.Base object at
 0x7fb54ae544d0>'
 sage: str(Derived())
 'good'
 }}}

 This also explains why the `__len__ = cardinality` in
 `src/sage/rings/finite_rings/finite_field_base.pyx` works. It doesn't work
 on the level of `sage.rings.finite_rings.finite_field_base.FiniteField`,
 but it works at the level of the actual finite field classes (which are
 Python classes).

--

--
Ticket URL: <http://trac.sagemath.org/ticket/19733#comment:7>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to