-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeywordArguments.py
More file actions
executable file
·86 lines (56 loc) · 1.14 KB
/
KeywordArguments.py
File metadata and controls
executable file
·86 lines (56 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 23:13:48 2021
@author: maherme
"""
#%%
def func1(a, b, *args, d):
print(a, b, args, d)
func1(1, 2, 3, 4, 5) # This will fail due to args run out the parameters
#%%
# You need to use keyword:
func1(1, 2, 3, 4, d=5)
#%%
# the * set the last positional parameter:
def func(*, d):
print(d)
func(d=100)
func(1, 2, d=100) # This will fail
#%%
def func(a, b, *, d):
print(a, b, d)
func(1, 2, d=4)
#%%
def func(a, b=2, *args, d):
print(a, b, args, d)
func(1, 5, 3, 4, d='a')
#%%
def func(a, b=20, *args, d=0, e):
print(a, b, args, d, e)
func(5, 4, 3, 2, 1, e='hello') # d is not mandatory
func(0, 600, d='hello', e='python')
func(11, 'm/s', 24, 'mph', d='unladen', e='swallow')
#%%
def func(**others):
print(others)
func(a=1, b=2, c=3)
#%%
def func(*args, **kwargs):
print(args)
print(kwargs)
func(1, 2, x=100, y=200)
#%%
def func(a, b, *, d, **kwargs):
print(a)
print(b)
print(d)
print(kwargs)
func(1, 2, x=100, y=200, d=20)
#%%
def func(a, b, **kwargs):
print(a)
print(b)
print(kwargs)
func(1, 2, x=100, y=200)
#%%