-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnpackingArguments.py
More file actions
executable file
·82 lines (61 loc) · 1.15 KB
/
UnpackingArguments.py
File metadata and controls
executable file
·82 lines (61 loc) · 1.15 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 22:53:08 2021
@author: maherme
"""
#%%
def func1(a, b, *args):
print(a)
print(b)
print(args)
func1(10, 20)
func1(10, 20, 1, 2, 3)
#%%
def avg(*args):
count = len(args)
total = sum(args)
return total/count
a = avg(2, 2, 4, 4)
print(a)
#%%
# Notice this will fail:
avg()
#%%
# Let's fix this:
def avg(*args):
count = len(args)
total = sum(args)
return count and total/count
a = avg(2, 2, 4, 4)
print(a)
a = avg()
print(a)
#%%
# This is another way to do the function, in this way a minimum of one
# parameter is required, otherwise will fail:
def avg(a, *args):
count = len(args) + 1
total = sum(args) + a
return total/count
a = avg(2, 2, 4, 4)
print(a)
a = avg()
print(a)
#%%
def func1(a, b, c):
print(a)
print(b)
print(c)
l = [10, 20, 30]
func1(*l) # if you do func1(l), this will fail because you are passing only 1 parameter
#%%
# If you can support whatever size of the list, with a minimum value:
def func1(a, b, c, *args):
print(a)
print(b)
print(c)
print(args)
l = [10, 20, 30, 40, 50]
func1(*l)
#%%