-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path8_functions.py
80 lines (53 loc) · 1.59 KB
/
8_functions.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
'''
Function
a function is a self-contained block of code that encapsulates a specific task or related group of tasks
'''
def my_func():
print("This is my function.")
my_func()
def my_func():
"""
This is a dummy function.
"""
print("This is my function.")
'''
Why do we need them?
- Code Reusability
- Chunking
Breaking a large task into smaller, bite-sized sub-tasks helps make the large task easier to think about and manage.
As programs become more complicated, it becomes increasingly beneficial to modularize them in this way.
- ETL
'''
def after_tax(price):
print(price * 1.1)
def after_tax(price, tax_percent):
print(price * (1 + (tax_percent/100)))
def after_tax(price, tax_percent=10):
print(price * (1 + (tax_percent/100)))
def after_tax(price, tax_percent):
return price * (1 + (tax_percent/100))
#When a parameter name in a Python function definition is preceded by an asterisk (*), it indicates argument tuple packing
*args
def multiply(*args):
print("Parameters passed in", args)
print("Type of args", type(args))
num = 1
for x in args:
num = num * x
return num
**kwargs
def sample_func(**kwargs):
print("Parameters passed in", kwargs)
print("Type of kwargs", type(kwargs))
for k, v in kwargs.items():
print(key, val)
#Python has a similar operator, the double asterisk (**), which can be used with Python function parameters and arguments to specify dictionary packing and unpacking.
def say(msg):
print(msg)
def saytwice(msg):
print(msg*2)
def wrapper_func(func):
msg = "Hello."
func(msg)
wrapper_func(say)
wrapper_func(saytwice)