Element Wrapper#

Wrapping Sage or Python objects as Sage elements.


  • Nicolas Thiery (2008-2010): Initial version

  • Travis Scrimshaw (2013-05-04): Cythonized version

class sage.structure.element_wrapper.DummyParent(name)#

Bases: UniqueRepresentation, Parent

A class for creating dummy parents for testing ElementWrapper

class sage.structure.element_wrapper.ElementWrapper#

Bases: Element

A class for wrapping Sage or Python objects as Sage elements.


sage: from sage.structure.element_wrapper import DummyParent
sage: parent = DummyParent("A parent")
sage: o = ElementWrapper(parent, "bla"); o
sage: isinstance(o, sage.structure.element.Element)
sage: o.parent()
A parent
sage: o.value

Note that o is not an instance of str, but rather contains a str. Therefore, o does not inherit the string methods. On the other hand, it is provided with reasonable default implementations for equality testing, hashing, etc.

The typical use case of ElementWrapper is for trivially constructing new element classes from pre-existing Sage or Python classes, with a containment relation. Here we construct the tropical monoid of integers endowed with min as multiplication. There, it is desirable not to inherit the factor method from Integer:

sage: class MinMonoid(Parent):
....:     def _repr_(self):
....:         return "The min monoid"
sage: M = MinMonoid()
sage: class MinMonoidElement(ElementWrapper):
....:     wrapped_class = Integer
....:     def __mul__(self, other):
....:         return MinMonoidElement(self.parent(), min(self.value, other.value))
sage: x = MinMonoidElement(M, 5); x
sage: x.parent()
The min monoid
sage: x.value
sage: y = MinMonoidElement(M, 3)
sage: x * y

This example was voluntarily kept to a bare minimum. See the examples in the categories (e.g. Semigroups().example()) for several full featured applications.


Versions before github issue #14519 had parent as the second argument and the value as the first.

class sage.structure.element_wrapper.ElementWrapperCheckWrappedClass#

Bases: ElementWrapper

An element wrapper such that comparison operations are done against subclasses of wrapped_class.


alias of object

class sage.structure.element_wrapper.ElementWrapperTester#

Bases: ElementWrapper

Test class for the default __copy() method of subclasses of ElementWrapper.