You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/advanced-python/decorator-kwargs-args.md
+20-20
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ Functions in Python are so called first class objects, which means they can be t
6
6
7
7
```python
8
8
deffunc1():
9
-
deffunc2():
10
-
print("Printing from the inner function, func2")
11
-
return func2
9
+
deffunc2():
10
+
print("Printing from the inner function, func2")
11
+
return func2
12
12
13
13
```
14
14
Assigning func1 to function_call object
@@ -17,7 +17,7 @@ function_call=func1()
17
17
```
18
18
Calling the function
19
19
```python
20
-
>> function_call()
20
+
>>> function_call()
21
21
```
22
22
**Output**
23
23
```
@@ -50,8 +50,8 @@ Now that you have understood why \* is used, we can take a look at *args. *args
50
50
*args makes python functions flexible to handle dynamic arguments.
51
51
```python
52
52
deftest1(*args):
53
-
print(args)
54
-
print(f"The number of elements in args = {len(args)}")
53
+
print(args)
54
+
print(f"The number of elements in args = {len(args)}")
55
55
a=list(range(0,10))
56
56
test1(*a)
57
57
```
@@ -64,8 +64,8 @@ If in the test1 we do not use \* in the argument
64
64
65
65
```python
66
66
deftest1(*args):
67
-
print(args)
68
-
print(f"The number of elements in args = {len(args)}")
67
+
print(args)
68
+
print(f"The number of elements in args = {len(args)}")
69
69
a=list(range(0,10))
70
70
test1(a)
71
71
```
@@ -78,8 +78,8 @@ The number of elements in args = 1
78
78
**kwargs stands for keyword arguments. This is used for key and value pairs and similar to *args, this makes functions flexible enough to handle dynamic key value pairs in arguments.
79
79
```python
80
80
deftest2(**kwargs):
81
-
print(kwargs)
82
-
print(f"The number of elements in kwargs = {len(kwargs)}")
81
+
print(kwargs)
82
+
print(f"The number of elements in kwargs = {len(kwargs)}")
83
83
test2(a=1,b=2,c=3,d=4,e=5)
84
84
```
85
85
The above snippet uses some key-value pairs and out test2 function gives the following output:
@@ -96,18 +96,18 @@ Now that we understand what first class object, *args, **kwargs is, we can move
96
96
```python
97
97
import time
98
98
defmultiplication(a,b):
99
-
start=time.time()
100
-
c=a*b
101
-
total=time.time()-start
102
-
print("Time taken for execution of multiplication",total)
103
-
return c
99
+
start=time.time()
100
+
c=a*b
101
+
total=time.time()-start
102
+
print("Time taken for execution of multiplication",total)
103
+
return c
104
104
105
105
defaddition(a,b):
106
-
start=time.time()
107
-
c=a+b
108
-
total=time.time()-start
109
-
print("Time taken for execution of addition ",total)
110
-
return c
106
+
start=time.time()
107
+
c=a+b
108
+
total=time.time()-start
109
+
print("Time taken for execution of addition ",total)
0 commit comments