Skip to content

SetMut

Bases: Set[T], MutableSet[T]

A mutable set wrapper with functional API.

Unlike Set which is immutable, SetMut allows in-place modification of elements.

Implement the MutableSet interface, so elements can be modified in place, and passed to any function/object expecting a standard mutable set.

Underlying data structure is a set.

Tip

If you have an existing set, prefer using SetMut.from_ref() to avoid unnecessary copying.

Parameters:

Name Type Description Default
data Iterable[T]

The mutable set to wrap.

required
Source code in src/pyochain/_iter.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
class SetMut[T](Set[T], MutableSet[T]):
    """A mutable `set` wrapper with functional API.

    Unlike `Set` which is immutable, `SetMut` allows in-place modification of elements.

    Implement the `MutableSet` interface, so elements can be modified in place, and passed to any function/object expecting a standard mutable `set`.

    Underlying data structure is a `set`.

    Tip:
        If you have an existing `set`, prefer using `SetMut.from_ref()` to avoid unnecessary copying.

    Args:
        data (Iterable[T]): The mutable set to wrap.
    """

    __slots__ = ()
    _inner: set[T]  # type: ignore[override]

    def __init__(self, data: Iterable[T]) -> None:
        self._inner = set(data)  # type: ignore[override]

    @staticmethod
    def from_ref[V](data: set[V]) -> SetMut[V]:
        """Create a `SetMut` from a reference to an existing `set`.

        This method wraps the provided `set` without copying it, allowing for efficient object instanciation.

        This is the recommended way to create a `SetMut` from foreign functions that return `set` objects.

        Warning:
            Since the `SetMut` directly references the original `set`, any modifications made to the `SetMut` will also affect the original `set`, and vice versa.

        Args:
            data (set[V]): The `set` to wrap.

        Returns:
            SetMut[V]: A new `SetMut` instance wrapping the provided `set`.

        Example:
        ```python
        >>> import pyochain as pc
        >>> original_set = {1, 2, 3}
        >>> set_obj = pc.SetMut.from_ref(original_set)
        >>> set_obj
        SetMut(1, 2, 3)
        >>> original_set.add(4)
        >>> set_obj
        SetMut(1, 2, 3, 4)


        ```
        """
        instance: SetMut[V] = SetMut.__new__(SetMut)  # pyright: ignore[reportUnknownVariableType]
        instance._inner = data
        return instance

    def add(self, value: T) -> None:
        """Add an element to **self**.

        Args:
            value (T): The element to add.

        Example:
        ```python
        >>> import pyochain as pc
        >>> s = pc.SetMut({'a', 'b'})
        >>> s.add('c')
        >>> s.iter().sort()
        Vec('a', 'b', 'c')

        ```
        """
        self._inner.add(value)

    def discard(self, value: T) -> None:
        """Remove an element from **self** if it is a member.

        Unlike `.remove()`, the `discard()` method does not raise an exception when an element is missing from the set.

        Args:
            value (T): The element to remove.

        Example:
        ```python
        >>> import pyochain as pc
        >>> s = pc.SetMut({'a', 'b', 'c'})
        >>> s.discard('b')
        >>> s.iter().sort()
        Vec('a', 'c')

        ```
        """
        self._inner.discard(value)

add(value)

Add an element to self.

Parameters:

Name Type Description Default
value T

The element to add.

required

Example:

>>> import pyochain as pc
>>> s = pc.SetMut({'a', 'b'})
>>> s.add('c')
>>> s.iter().sort()
Vec('a', 'b', 'c')

Source code in src/pyochain/_iter.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
def add(self, value: T) -> None:
    """Add an element to **self**.

    Args:
        value (T): The element to add.

    Example:
    ```python
    >>> import pyochain as pc
    >>> s = pc.SetMut({'a', 'b'})
    >>> s.add('c')
    >>> s.iter().sort()
    Vec('a', 'b', 'c')

    ```
    """
    self._inner.add(value)

discard(value)

Remove an element from self if it is a member.

Unlike .remove(), the discard() method does not raise an exception when an element is missing from the set.

Parameters:

Name Type Description Default
value T

The element to remove.

required

Example:

>>> import pyochain as pc
>>> s = pc.SetMut({'a', 'b', 'c'})
>>> s.discard('b')
>>> s.iter().sort()
Vec('a', 'c')

Source code in src/pyochain/_iter.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def discard(self, value: T) -> None:
    """Remove an element from **self** if it is a member.

    Unlike `.remove()`, the `discard()` method does not raise an exception when an element is missing from the set.

    Args:
        value (T): The element to remove.

    Example:
    ```python
    >>> import pyochain as pc
    >>> s = pc.SetMut({'a', 'b', 'c'})
    >>> s.discard('b')
    >>> s.iter().sort()
    Vec('a', 'c')

    ```
    """
    self._inner.discard(value)

from_ref(data) staticmethod

Create a SetMut from a reference to an existing set.

This method wraps the provided set without copying it, allowing for efficient object instanciation.

This is the recommended way to create a SetMut from foreign functions that return set objects.

Warning

Since the SetMut directly references the original set, any modifications made to the SetMut will also affect the original set, and vice versa.

Parameters:

Name Type Description Default
data set[V]

The set to wrap.

required

Returns:

Type Description
SetMut[V]

SetMut[V]: A new SetMut instance wrapping the provided set.

Example:

>>> import pyochain as pc
>>> original_set = {1, 2, 3}
>>> set_obj = pc.SetMut.from_ref(original_set)
>>> set_obj
SetMut(1, 2, 3)
>>> original_set.add(4)
>>> set_obj
SetMut(1, 2, 3, 4)

Source code in src/pyochain/_iter.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@staticmethod
def from_ref[V](data: set[V]) -> SetMut[V]:
    """Create a `SetMut` from a reference to an existing `set`.

    This method wraps the provided `set` without copying it, allowing for efficient object instanciation.

    This is the recommended way to create a `SetMut` from foreign functions that return `set` objects.

    Warning:
        Since the `SetMut` directly references the original `set`, any modifications made to the `SetMut` will also affect the original `set`, and vice versa.

    Args:
        data (set[V]): The `set` to wrap.

    Returns:
        SetMut[V]: A new `SetMut` instance wrapping the provided `set`.

    Example:
    ```python
    >>> import pyochain as pc
    >>> original_set = {1, 2, 3}
    >>> set_obj = pc.SetMut.from_ref(original_set)
    >>> set_obj
    SetMut(1, 2, 3)
    >>> original_set.add(4)
    >>> set_obj
    SetMut(1, 2, 3, 4)


    ```
    """
    instance: SetMut[V] = SetMut.__new__(SetMut)  # pyright: ignore[reportUnknownVariableType]
    instance._inner = data
    return instance