Skip to content

Commit 84bd454

Browse files
committed
Add OrderedSet.of factory method
A convenient shorthand for OrderedSet([*args]), which avoids an extra pair of brackets.
1 parent a050e3e commit 84bd454

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/techcable/orderedset/_orderedset.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@ def __init__(self, source: Iterable[T] | None = None, /) -> None:
8282
self.append(value)
8383
assert len(self._unique) == len(self._elements)
8484

85+
@classmethod
86+
def of(cls, /, *args: T) -> OrderedSet[T]:
87+
"""
88+
Construct an [`OrderedSet`] using specified elements.
89+
90+
This is a factory method equivalent to `OrderedSet([*args])`,
91+
but avoiding an extra pair of brackets.
92+
93+
It is inspired by Java's [`List.of`] method.
94+
95+
Unlike the java method, the returned set is not immutable.
96+
It is no different from those produced using the standard constructor.
97+
98+
[`List.of`]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#of(E...)
99+
"""
100+
return cls(args)
101+
85102
def append(self, value: T, /) -> bool:
86103
"""Append a value to the set if it doesn't already exist.
87104

0 commit comments

Comments
 (0)