-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanypositional.py
More file actions
77 lines (65 loc) · 2.72 KB
/
manypositional.py
File metadata and controls
77 lines (65 loc) · 2.72 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
def func(*args):
print(args)
# You can see I can send multi positional argument as per function argument definition above
func("arg1", "arg2", "arg3", "arg4")
"""
It prints our a tuple so the as below
('arg1', 'arg2', 'arg3', 'arg4')
"""
# if i want to print a specify element in the list
def func1(*args):
print(args[0])
func1("arg1", "arg2", "arg3", "arg4")
# ----------------------------------------------------------------------------------------------------
# def add_numbers(*numbers):
# total = sum(numbers)
# print(f"Total: {total}")
# add_numbers(10, 20, 30)
# # Let us experiment with *args by putting in with other parameters as below
# def print_varying_members(member1, member2, *args, member3):
# print(f"member1 is {member1}")
# print(f"member2 is {member2}")
# print(f"member3 is {member3}")
# print(f"*args contains {args}")
# # Let us call the function above
# print_varying_members("Frank", member2="Dean", member3="Sammy")
# '''
# member1 is Frank
# member2 is Dean
# member3 is Sammy
# *args contains () --> args tuple is empty because you didn’t pass any additional arguments beyond the defined parameters.
# '''
# # Let me call by using "member2" parameter as positional argument
# print_varying_members("Frank", "Dean", member3="Sammy")
# '''
# --> You see same result nothing changed
# member1 is Frank
# member2 is Dean
# member3 is Sammy
# *args contains () --> args tuple is empty because you didn’t pass any additional arguments beyond the defined parameters.
# '''
# # Let us add additional parameter
# print_varying_members("Frank", "Dean", "arg2", "arg3", member3="Sammy")
# '''
# member1 is Frank
# member2 is Dean
# member3 is Sammy
# *args contains ('arg2', 'arg3') --> you see the args tuple is now note empty since you passed additional positional arguments
# '''
# # Let us make the last arguments as positional argument call
# print_varying_members("Frank", "Dean", "Sammy") # Argument missing for parameter "member3"
# '''
# You can see that it assume the first parameter and the second as positional arguments until it
# reach the last argument "Sammy" which it will be taken by the multi positional argument definition (*args)
# and after that it find out the the parameter "member3" is missing
# '''
# # same issue as above since since "Sammy" will be taken into the "*args" or the multi positional argument parameter definition
# print_varying_members("Frank", member2="Dean", "Sammy") # Argument missing for parameter "member3"
# '''
# "Sammy" argument as positional is taken by the "*args"
# '''
# # Let us try this
# print_varying_members(member1="Frank", "Dean", member3="Sammy") # Argument missing for parameter "member2"
# '''
# "Dean" argument as positional is taken by the "*args"
# '''