@@ -434,9 +434,23 @@ now.strftime('%Y%m%d%H%M%S') # '20180315002834'
434
434
```
435
435
436
436
437
- Splat Operator
438
- --------------
437
+ Arguments
438
+ ---------
439
439
### Inside Function Call
440
+ ``` python
441
+ < function> (< positional_args> ) # f(0, 0)
442
+ < function> (< keyword_args> ) # f(x=0, y=0)
443
+ < function> (< positional_args> , < keyword_args> ) # f(0, y=0)
444
+ ```
445
+
446
+ ### Inside Function Definition
447
+ ``` python
448
+ def f (<nondefault_args>) # def f(x, y)
449
+ def f (<default_args>) # def f(x=0, y=0)
450
+ def f (<nondefault_args>, <default_args>) # def f(x, y=0)
451
+ ```
452
+
453
+ ### Splat operator
440
454
** ` '*' ` is the splat operator, that takes a collection as input, and expands it into actual positional arguments in the function call.**
441
455
442
456
``` python
@@ -450,8 +464,7 @@ func(*args, **kwargs)
450
464
func(1 , 2 , x = 3 , y = 4 , z = 5 )
451
465
```
452
466
453
- ### Inside Function Declaration
454
- #### Example:
467
+ #### Splat example:
455
468
``` python
456
469
def add (* a ):
457
470
return sum (a)
@@ -462,24 +475,24 @@ def add(*a):
462
475
6
463
476
```
464
477
465
- #### Legal uses:
478
+ ### Legal Argument Definitions and Calls
466
479
``` 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)
480
+ def f (* args ) # f(1, 2, 3)
481
+ def f (x , * args ) # f(1, 2, 3)
482
+ def f (* args , z ) # f(1, 2, z=3)
483
+ def f (x , * args , z ) # f(1, 2, z=3)
471
484
```
472
485
473
486
``` 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)
487
+ def f (** kwargs ) # f(x=1, y=2, z=3)
488
+ def f (x , ** kwargs ) # f(x=1, y=2, z=3) | f(1, y=2, z=3)
476
489
```
477
490
478
491
``` 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)
492
+ def f (* args , ** kwargs ) # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
493
+ def f (x , * args , ** kwargs ) # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3) | f(1, 2, 3)
494
+ def f (* args , y , ** kwargs ) # f(x=1, y=2, z=3) | f(1, y=2, z=3)
495
+ def f (x , * args , z , ** kwargs ) # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=3)
483
496
```
484
497
485
498
### Other Uses
@@ -1006,7 +1019,7 @@ from argparse import ArgumentParser, FileType
1006
1019
< parser> .add_argument(' -<short_name>' , ' --<name>' , action = ' store_true' ) # Flag
1007
1020
< parser> .add_argument(' -<short_name>' , ' --<name>' , type = < type > ) # Option
1008
1021
< parser> .add_argument(' <name>' , type = < type > , nargs = 1 ) # First argument
1009
- < parser> .add_argument(' <name>' , type = < type > , nargs = ' +' ) # Ramaining arguments
1022
+ < parser> .add_argument(' <name>' , type = < type > , nargs = ' +' ) # Remaining arguments
1010
1023
< args> = < parser> .parse_args()
1011
1024
value = < args> .< name>
1012
1025
```
0 commit comments