-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunpacking-arguments.py
More file actions
56 lines (39 loc) · 982 Bytes
/
unpacking-arguments.py
File metadata and controls
56 lines (39 loc) · 982 Bytes
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
# similar to rest parameter in js
def mult(*args):
print(args)
total = 1
for arg in args:
total *= arg
return total
print(mult(1,2,3,4))
arr = [2,3,4]
def multi(x,y,z):
return x*y*z
# similar to spread operator
print(multi(*arr))
obj = {"x":15, "y":25, "z":20}
print(multi(**obj))
test = (1,2,3,4)
print(mult(*test))
def apply(*args, operator):
if operator == "*":
return mult(*args)
elif operator == "+":
return sum(args)
else:
return "not valid operator"
print(apply(1,2,3,4,5,operator="*"))
# collecting arguments
def createDic(**kwargs):
print(kwargs)
createDic(name="Bob", age=15)
def details(name,age):
print(f"{name} {age}")
me = {"name":"min", "age":27}
details(**me)
# ** when use in function definition serves as collect when used in function invocation serves as unpack.
def printName(**kwargs):
print(kwargs.items())
for key,value in kwargs.items():
print(f"{key} : {value} ")
printName(name="Min", age="28")