@@ -434,9 +434,10 @@ now.strftime('%Y%m%d%H%M%S') # '20180315002834'
434
434
```
435
435
436
436
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.**
440
441
441
442
``` python
442
443
args = (1 , 2 )
@@ -449,7 +450,8 @@ func(*args, **kwargs)
449
450
func(1 , 2 , x = 3 , y = 4 , z = 5 )
450
451
```
451
452
452
- #### Splat operator can also be used in function declarations:
453
+ ### Inside Function Declaration
454
+ #### Example:
453
455
``` python
454
456
def add (* a ):
455
457
return sum (a)
@@ -460,7 +462,27 @@ def add(*a):
460
462
6
461
463
```
462
464
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
464
486
``` python
465
487
>> > a = (1 , 2 , 3 )
466
488
>> > [* a]
0 commit comments