-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositional_keyword_arguments.py
103 lines (87 loc) · 4.85 KB
/
positional_keyword_arguments.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
def zoo(
pos_only_no_default, # Positional-only argument, no default
/, # All preceding arguments are positional-only
pos_or_kwarg_no_default, # Can be passed either positionally or as a keyword, no default
pos_or_kwarg_with_default="default_value", # Can be passed either positionally or as a keyword, with default
*args, # Variable positional arguments # All following arguments are keyword-only
kwarg_only_no_default, # Keyword-only argument, no default
kwarg_only_with_default="default_kwarg", # Keyword-only argument, with default
**kwargs, # Variable keyword arguments
):
print("pos_only_no_default:", pos_only_no_default)
print("pos_or_kwarg_no_default:", pos_or_kwarg_no_default)
print("pos_or_kwarg_with_default:", pos_or_kwarg_with_default)
print("kwarg_only_no_default:", kwarg_only_no_default)
print("kwarg_only_with_default:", kwarg_only_with_default)
print("Additional positional arguments (*args):", args)
print("Additional keyword arguments (**kwargs):", kwargs)
zoo(1, 2, 3, 4, 5, 6, kwarg_only_no_default="a", b="b", c="c")
def test_2():
def foo(
pos_only_no_default, # Positional-only argument, no default
/, # All preceding are positional-only
pos_or_kwarg_no_default, # Can be passed either positionally or as a keyword, no default
pos_or_kwarg_with_default="default_value", # Can be passed either positionally or as a keyword, with default
*, # All following arguments are keyword-only
kwarg_only_no_default, # Keyword-only argument, no default
kwarg_only_with_default="default_kwarg", # Keyword-only argument, with default
):
"""
A function demonstrating all combinations of positional-only, positional-or-keyword,
and keyword-only arguments with and without default values.
"""
print(f"pos_only_no_default: {pos_only_no_default}")
print(f"pos_or_kwarg_no_default: {pos_or_kwarg_no_default}")
print(f"pos_or_kwarg_with_default: {pos_or_kwarg_with_default}")
print(f"kwarg_only_no_default: {kwarg_only_no_default}")
print(f"kwarg_only_with_default: {kwarg_only_with_default}")
# Example calls:
# 1. Positional arguments only (default for pos_or_kwarg_with_default will be used)
foo(1, 3, kwarg_only_no_default=6)
# 2. Using positional arguments and keyword arguments (pos_or_kwarg_with_default will use its default value)
foo(1, 3, kwarg_only_no_default=6)
# 3. Passing both positional and keyword arguments explicitly (overriding defaults)
foo(
1,
3,
"non_default",
kwarg_only_no_default=6,
kwarg_only_with_default="non_default_kwarg",
)
# 4. If you want to rely on more default values
foo(
1, 3, kwarg_only_no_default=6
) # pos_or_kwarg_with_default and kwarg_only_with_default use defaults
def test_3():
def bar(
# pos_only_no_default, # Positional-only argument, no default
pos_only_with_default=None, # Positional-only argument, with default
/, # All preceding are positional-only
# pos_or_kwarg_no_default, # Can be passed either positionally or as a keyword, no default NOT ALLOWED
pos_or_kwarg_with_default="default_value", # Can be passed either positionally or as a keyword, with default
*, # All following arguments are keyword-only
kwarg_only_no_default, # Keyword-only argument, no default
kwarg_only_with_default="default_kwarg", # Keyword-only argument, with default
):
"""
A function demonstrating all combinations of positional-only, positional-or-keyword,
and keyword-only arguments with and without default values.
"""
# print(f"pos_only_no_default: {pos_only_no_default}")
print(f"pos_only_with_default: {pos_only_with_default}")
## print(f"pos_or_kwarg_no_default: {pos_or_kwarg_no_default}")
print(f"pos_or_kwarg_with_default: {pos_or_kwarg_with_default}")
print(f"kwarg_only_no_default: {kwarg_only_no_default}")
print(f"kwarg_only_with_default: {kwarg_only_with_default}")
# Example calls:
# 1. Positional arguments only (default for pos_only_with_default will be used)
bar(1, None, 3, kwarg_only_no_default=6)
# 2. Using positional arguments and keyword arguments (pos_or_kwarg_with_default will use its default value)
bar(1, 2, 3, kwarg_only_no_default=6)
# 3. Passing both positional and keyword arguments explicitly (overriding defaults)
# bar(1, 2, 3, 'non_default', kwarg_only_no_default=6, kwarg_only_with_default='non_default_kwarg')
bar(1, 2, 3, kwarg_only_no_default=6, kwarg_only_with_default="non_default_kwarg")
# 4. If you want to rely on more default values
bar(
1, 2, 3, kwarg_only_no_default=6
) # pos_or_kwarg_with_default and kwarg_only_with_default use defaults