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

WARNING: This class has serious issues that can lead to subtle segfaults. Do not use it unless you read trac 8200 first: http://trac.sagemath.org/sage_trac/ticket/8200

class sage.structure.element_wrapper.DummyParent(name)

Bases: sage.structure.unique_representation.UniqueRepresentation, sage.structure.parent.Parent

A class for creating dummy parents for testing ElementWrapper

class sage.structure.element_wrapper.ElementWrapper(value, parent)

Bases: sage.structure.element.Element

A class for wrapping Sage or Python objects as Sage elements

WARNING: This class has serious issues that can lead to subtle segfaults. Do not use it unless you read trac 8200 first: http://trac.sagemath.org/sage_trac/ticket/8200

EXAMPLES:

sage: from sage.structure.element_wrapper import DummyParent
sage: parent = DummyParent("A parent")

sage: o = ElementWrapper("bla", parent = parent); o
'bla'
sage: isinstance(o, sage.structure.element.Element)
True
sage: o.parent()
A parent
sage: o.value
'bla'

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 preexisting 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(min(self.value, other.value), parent = self.parent())
sage: x = MinMonoidElement(5, parent = M); x
5
sage: x.parent()
The min monoid
sage: x.value
5
sage: y = MinMonoidElement(3, parent = M)
sage: 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.

Caveat: the order between the value and the parent argument is likely to change shortly. At this point, all the code using it in the Sage library will be updated. There will be no transition period.

class sage.structure.element_wrapper.ElementWrapperTester

Bases: sage.structure.element_wrapper.ElementWrapper

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

TESTS:

sage: from sage.structure.element_wrapper import ElementWrapperTester
sage: x = ElementWrapperTester()
sage: x.append(2); y = copy(x); y.append(42)
sage: type(y)
<class 'sage.structure.element_wrapper.ElementWrapperTester'>
sage: x, y
([n=1, value=[2]], [n=2, value=[2, 42]])
sage: x.append(21); x.append(7)
sage: x, y
([n=3, value=[2, 21, 7]], [n=2, value=[2, 42]])
sage: x.__dict__, y.__dict__
({'value': [2, 21, 7], 'n': 3}, {'value': [2, 42], 'n': 2})
append(x)

TESTS:

sage: from sage.structure.element_wrapper import ElementWrapperTester
sage: x = ElementWrapperTester()
sage: x.append(2); x
[n=1, value=[2]]

Previous topic

Sequences

Next topic

Cartesian products

This Page