### Navigation

- index
- modules |
- next |
- previous |
- Basic Structures »

# Element Wrapper

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)

  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`

  Bases: `sage.structure.element.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'

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

  Warning

  Versions before trac ticket #14519 had parent as the second argument and the value as the first.

  `value`

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.value, y.value
      ([2, 21, 7], [2, 42])
      sage: x.__dict__, y.__dict__
      ({'n': 3}, {'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]]

### Navigation

- index
- modules |
- next |
- previous |
- Basic Structures »