Skip to content

Set

Bases: PyoSet[T]

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

Implements the Collection Protocol from collections.abc, 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, prefer using SetMut.from_ref() to avoid unnecessary copying.

Parameters:

Name Type Description Default
data Iterable[T]

The data to initialize the Set with.

required
Source code in src/pyochain/_iter.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class Set[T](PyoSet[T]):
    """`Set` represent an in- memory **unordered**  collection of **unique** elements.

    Implements the `Collection` Protocol from `collections.abc`, 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`, prefer using `SetMut.from_ref()` to avoid unnecessary copying.

    Args:
            data (Iterable[T]): The data to initialize the Set with.
    """

    __slots__ = ("_inner",)
    __match_args__ = ("_inner",)
    _inner: frozenset[T]

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

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

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

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

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