Hi John, On 2013-01-05, john_perry_usm <john.pe...@usm.edu> wrote: > thinking of how java interfaces & multiple inheritance work: > wouldn't it be possible to have an interface orabstract class > that defines an abstract function abs, along with associated > functions that require an abs functions, then declare in the > class header that it implements it, and define the function so?
The category framework offers such functionality. For example, one could define the following in sage.categories.rings: from sage.misc.abstract_method import abstract_method class Rings(Category): ... class ElementMethods: @abstract_method(optional=True) def abs(self): """ Return the absolute value. Implement only if that makes sense in this ring. """ pass and then, for example: sage: from sage.structure.element import Element sage: from sage.structure.parent import Parent sage: class MyRingElement(Element): ....: def _mul_(self, other): ....: return self ....: sage: class MyRingElementWithAbs(MyRingElement): ....: def abs(self): ....: return self ....: sage: class MyRing(Parent): ....: def __init__(self, has_abs=None): ....: if has_abs: ....: self.Element = MyRingElementWithAbs ....: else: ....: self.Element = MyRingElement ....: Parent.__init__(self, category=Rings()) ....: def _element_constructor_(self, *args, **kwds): ....: return self.element_class(parent=self) ....: sage: R = MyRing() sage: RAbs = MyRing(has_abs=True) sage: a = R() sage: b = RAbs() sage: b.abs <bound method MyRing_with_category.element_class.abs of Generic element of a structure> sage: b.abs() Generic element of a structure sage: hasattr(a, 'abs') # yes, still available by tab completion True sage: a.abs # but that's good: NotImplemented sage: a.__class__.abs? ... Definition: a.__class__.abs(self) Docstring: Return the absolute value. Implement only if that makes sense in this ring. ... I guess the main problem is that the abs() method of sage.structure.element.RingElement has been introduced long before the category framework was created. But we could consider to move abs() from the base class RingElement to the element class of the category of rings. The question is how to deal with old code, that tries to implement abs() by means of __abs__(). Best regards, Simon -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To post to this group, send email to sage-devel@googlegroups.com. To unsubscribe from this group, send email to sage-devel+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel?hl=en.