mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-14 23:51:06 +00:00
41 lines
874 B
Markdown
41 lines
874 B
Markdown
# Spread assignment
|
|
|
|
In a decomposing assignment, putting `...` in front of a variable expands all remaining elements into that variable. This is called expansion assignment.
|
|
|
|
```python
|
|
[x, *y] = [1, 2, 3]
|
|
assert x == 1
|
|
assert y == [2, 3]
|
|
x, *y = (1, 2, 3)
|
|
assert x == 1
|
|
assert y == (2, 3)
|
|
```
|
|
|
|
## Extract assignment
|
|
|
|
Extraction assignment is a convenient syntax for localizing specific attributes within a module or record.
|
|
|
|
```python
|
|
{sin; cos; tan} = import "math"
|
|
```
|
|
|
|
After that, you can use `sin, cos, tan` locally.
|
|
|
|
You can do the same with records.
|
|
|
|
```python
|
|
record = {x = 1; y = 2}
|
|
{x; y} = record
|
|
```
|
|
|
|
If you want to expand all, use `{*} = record`. It is `open` in OCaml.
|
|
|
|
```python
|
|
record = {x = 1; y = 2}
|
|
{*} = records
|
|
assert x == 1 and y == 2
|
|
```
|
|
|
|
<p align='center'>
|
|
<a href='./29_comprehension.md'>Previous</a> | <a href='./31_decorator.md'>Next</a>
|
|
</p>
|