Element Wrapper#
Wrapping Sage or Python objects as Sage elements.
AUTHORS:
Nicolas Thiery (2008-2010): Initial version
Travis Scrimshaw (2013-05-04): Cythonized version
- class sage.structure.element_wrapper.DummyParent(name)[source]#
Bases:
UniqueRepresentation
,Parent
A class for creating dummy parents for testing
ElementWrapper
- class sage.structure.element_wrapper.ElementWrapper[source]#
Bases:
Element
A class for wrapping Sage or Python objects as Sage elements.
EXAMPLES:
sage: from sage.structure.element_wrapper import DummyParent sage: parent = DummyParent("A parent") sage: o = ElementWrapper(parent, "bla"); o 'bla' sage: isinstance(o, sage.structure.element.Element) True sage: o.parent() A parent sage: o.value 'bla'
>>> from sage.all import * >>> from sage.structure.element_wrapper import DummyParent >>> parent = DummyParent("A parent") >>> o = ElementWrapper(parent, "bla"); o 'bla' >>> isinstance(o, sage.structure.element.Element) True >>> o.parent() A parent >>> o.value 'bla'
Note that
o
is not an instance ofstr
, but rather contains astr
. 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 withmin
as multiplication. There, it is desirable not to inherit thefactor
method fromInteger
: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 5 sage: x.parent() The min monoid sage: x.value 5 sage: y = MinMonoidElement(M, 3) sage: x * y 3
>>> from sage.all import * >>> class MinMonoid(Parent): ... def _repr_(self): ... return "The min monoid" ....: >>> M = MinMonoid() >>> class MinMonoidElement(ElementWrapper): ... wrapped_class = Integer ....: >>> def __mul__(self, other): ... return MinMonoidElement(self.parent(), min(self.value, other.value)) >>> x = MinMonoidElement(M, Integer(5)); x 5 >>> x.parent() The min monoid >>> x.value 5 >>> y = MinMonoidElement(M, Integer(3)) >>> x * y 3
This example was voluntarily kept to a bare minimum. See the examples in the categories (e.g.
Semigroups().example()
) for several full featured applications.Warning
Versions before Issue #14519 had parent as the second argument and the value as the first.
- class sage.structure.element_wrapper.ElementWrapperCheckWrappedClass[source]#
Bases:
ElementWrapper
An
element wrapper
such that comparison operations are done against subclasses ofwrapped_class
.- wrapped_class#
alias of
object
- class sage.structure.element_wrapper.ElementWrapperTester[source]#
Bases:
ElementWrapper
Test class for the default
__copy()
method of subclasses ofElementWrapper
.