Skip to content

Set

Bases: PyoSet[T]


              flowchart TD
              pyochain._set.Set[Set]
              pyochain.abc._set.PyoSet[PyoSet]
              pyochain.abc._collection.PyoCollection[PyoCollection]
              pyochain.abc._iterable.PyoIterable[PyoIterable]
              pyochain.rs.Pipeable[Pipeable]
              pyochain.rs.Into[Into]
              pyochain.rs.Inspect[Inspect]
              pyochain.rs.Checkable[Checkable]
              pyochain.abc._collection.PyoContainer[PyoContainer]
              pyochain.abc._collection.PyoSized[PyoSized]

                              pyochain.abc._set.PyoSet --> pyochain._set.Set
                                pyochain.abc._collection.PyoCollection --> pyochain.abc._set.PyoSet
                                pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
                                pyochain.rs.Pipeable --> pyochain.abc._iterable.PyoIterable
                                pyochain.rs.Into --> pyochain.rs.Pipeable
                
                pyochain.rs.Inspect --> pyochain.rs.Pipeable
                

                pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
                

                pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
                
                pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
                




              click pyochain._set.Set href "" "pyochain._set.Set"
              click pyochain.abc._set.PyoSet href "" "pyochain.abc._set.PyoSet"
              click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
              click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
              click pyochain.rs.Pipeable href "" "pyochain.rs.Pipeable"
              click pyochain.rs.Into href "" "pyochain.rs.Into"
              click pyochain.rs.Inspect href "" "pyochain.rs.Inspect"
              click pyochain.rs.Checkable href "" "pyochain.rs.Checkable"
              click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
              click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
            

Set represent an in- memory unordered collection of unique elements.

Implements the collections::abc::Collection Protocol, so it can be used as a standard immutable collection.

The underlying data structure is a frozenset.

Tip
  • Set(frozenset) is a no-copy operation since Python optimizes this under the hood.
  • If you have an existing set, consider using SetMut::from_ref to avoid unnecessary copying.

Parameters:

Name Type Description Default
data Iterable[T]

Any Iterable of elements to initialize the set with.

required
Example
>>> from pyochain import Set
>>> Set(())
Set()
>>> s = Set((1, 2, 2, 3))
>>> s
Set(1, 2, 3)
>>> s_2 = Set(s.inner)
>>> # No copy is made when creating s_2 from s.inner, they reference the same underlying frozenset.
>>> is_no_copy = (
...     s.inner is s_2.inner
...     and s.inner is s.inner
...     and s_2.inner is s.inner
...     and frozenset(s.inner) is s.inner
... )
>>> is_no_copy
True
>>> # However, creating a new Set from s (not using .inner) will be a copy operation.
>>> Set(s).inner is s.inner
False
Source code in src/pyochain/_set.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
class Set[T](PyoSet[T]):
    """`Set` represent an in- memory **unordered**  collection of **unique** elements.

    Implements the `collections::abc::Collection` Protocol, so it can be used as a standard immutable collection.

    The underlying data structure is a `frozenset`.

    Tip:
        - `Set(frozenset)` is a no-copy operation since Python optimizes this under the hood.
        - If you have an existing `set`, consider using [`SetMut::from_ref`][SetMut.from_ref] to avoid unnecessary copying.

    Args:
        data (Iterable[T]): Any `Iterable` of elements to initialize the set with.

    Example:
        ```python
        >>> from pyochain import Set
        >>> Set(())
        Set()
        >>> s = Set((1, 2, 2, 3))
        >>> s
        Set(1, 2, 3)
        >>> s_2 = Set(s.inner)
        >>> # No copy is made when creating s_2 from s.inner, they reference the same underlying frozenset.
        >>> is_no_copy = (
        ...     s.inner is s_2.inner
        ...     and s.inner is s.inner
        ...     and s_2.inner is s.inner
        ...     and frozenset(s.inner) is s.inner
        ... )
        >>> is_no_copy
        True
        >>> # However, creating a new Set from s (not using .inner) will be a copy operation.
        >>> Set(s).inner is s.inner
        False

        ```
    """

    __slots__ = ("_inner",)  # pyright: ignore[reportUnannotatedClassAttribute, reportIncompatibleUnannotatedOverride]
    _inner: Final[frozenset[T]]

    def __init__(self, data: Iterable[T]) -> None:
        self._inner = frozenset(data)

    @property
    @no_doctest
    def inner(self) -> frozenset[T]:
        """Get the underlying `frozenset` data structure.

        Useful when interoperating with functions that require a standard Python `frozenset`.

        Returns:
            frozenset[T]: The underlying frozenset.
        """
        return self._inner

    @override
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({get_repr(self._inner)})"

    @override
    def __contains__(self, item: object) -> bool:
        return item in self._inner

    @override
    def __iter__(self) -> Iterator[T]:
        return iter(self._inner)

    @override
    def __len__(self) -> int:
        return len(self._inner)

    @override
    def __eq__(self, other: object) -> bool:
        return _set_eq(self, other)

    @override
    def __hash__(self) -> int:
        return hash(self._inner)

    @override
    def intersection(self, other: AbstractSet[T]) -> Self:
        return self.__class__(self._inner & other)

    @override
    def union(self, other: AbstractSet[T]) -> Self:
        return self.__class__(self._inner | other)

    @override
    def difference(self, other: AbstractSet[T]) -> Self:
        return self.__class__(self._inner - other)

    @override
    def symmetric_difference(self, other: AbstractSet[T]) -> Self:
        return self.__class__(self._inner ^ other)

inner property

Get the underlying frozenset data structure.

Useful when interoperating with functions that require a standard Python frozenset.

Returns:

Type Description
frozenset[T]

frozenset[T]: The underlying frozenset.