SetMut
Bases: ,
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
|
|
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 | |
add(value)
Add an element to self.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
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 | |
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
|
|
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 | |
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
|
|
The |
required |
Returns:
| Type | Description |
|---|---|
|
SetMut[V]: A new |
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 | |