Skip to content

Commit d789a92

Browse files
committed
Splat
1 parent ee6eeb6 commit d789a92

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,10 @@ now.strftime('%Y%m%d%H%M%S') # '20180315002834'
434434
```
435435

436436

437-
Arguments
438-
---------
439-
**`'*'` is the splat operator, that takes a list as input, and expands it into actual positional arguments in the function call.**
437+
Splat Operator
438+
--------------
439+
### Inside Function Call
440+
**`'*'` is the splat operator, that takes a collection as input, and expands it into actual positional arguments in the function call.**
440441

441442
```python
442443
args = (1, 2)
@@ -449,7 +450,8 @@ func(*args, **kwargs)
449450
func(1, 2, x=3, y=4, z=5)
450451
```
451452

452-
#### Splat operator can also be used in function declarations:
453+
### Inside Function Declaration
454+
#### Example:
453455
```python
454456
def add(*a):
455457
return sum(a)
@@ -460,7 +462,27 @@ def add(*a):
460462
6
461463
```
462464

463-
#### And in few other places:
465+
#### Legal uses:
466+
```python
467+
def f(*args): pass # f(1, 2, 3)
468+
def f(x, *args): pass # f(1, 2, 3)
469+
def f(*args, z): pass # f(1, 2, z=3)
470+
def f(x, *args, z): pass # f(1, 2, z=3)
471+
```
472+
473+
```python
474+
def f(**kwargs): pass # f(x=1, y=2, z=3)
475+
def f(x, **kwargs): pass # f(x=1, y=2, z=3) or f(1, y=2, z=3)
476+
```
477+
478+
```python
479+
def f(*args, **kwargs): pass # f(x=1, y=2, z=3) or f(1, y=2, z=3) or f(1, 2, z=3) or f(1, 2, 3)
480+
def f(x, *args, **kwargs): pass # f(x=1, y=2, z=3) or f(1, y=2, z=3) or f(1, 2, z=3) or f(1, 2, 3)
481+
def f(*args, y, **kwargs): pass # f(x=1, y=2, z=3) or f(1, y=2, z=3)
482+
def f(x, *args, z, **kwargs): pass # f(x=1, y=2, z=3) or f(1, y=2, z=3) or f(1, 2, z=3)
483+
```
484+
485+
### Other Uses
464486
```python
465487
>>> a = (1, 2, 3)
466488
>>> [*a]

0 commit comments

Comments
 (0)