On Fri, Mar 23, 2012 at 07:33:01PM -0700, David Roe wrote:
>    Unfortunately, whenever I write a Python class I've also been very
>    frustrated trying to get the class I'm writing to inherit from the
>    appropriate element_class.  As a consequence I try to write most of my
>    elements in Cython.  :-)
> 
>    The inheritance is set up to work correctly if you follow the paradigm of
>    just defining an ElementMethods class on the category (see basically any
>    category in Sage) and throwing methods into it.  You could just inherit
>    from Rings().element_class (for example), but this doesn't work very well
>    if you want to use one element class for elements of parents in different
>    categories (e.g. Z/NZ for both prime and composite N).
> 
>    Frankly, this is one of my biggest frustrations with the current category
>    framework.  But maybe I'm just missing something.  :-)

I think you are :-) At least, I very much hope so.

In principle all a parent P needs to do is to setup its category C in
__init__, and have P.Element be (or point to) the base class for its
elements. Then, P.element_class will inherit both from P.Element, and
from C.element_class. Note that C can typically depend on the
arguments passed down to __init__.

Of course, the construction of elements itself should use
P.element_class, not P.Element. This is what the default
implementation of _element_constructor_ does, so usually you need not
worry about it.

        sage: TestSuite(P).run()

will complain if things are not done right.

For plenty of examples, have a look at:

        sage: A = Sets().example()
        sage: A??

and friends.

Cheers,
                                Nicolas
--
Nicolas M. ThiƩry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/

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

Reply via email to