File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Understanding Args and Kwargs
3
+
4
+ What will be the output from the following functions and why
5
+ """
6
+
7
+ def f (x , y , z ):
8
+ # uncomment for debug
9
+ # print "x=%s y=%s, z=%s" % (x, y, z)
10
+ return (x + y ) / float (z )
11
+
12
+ print f (10 , 5 , 3 )
13
+
14
+ x = 20
15
+ y = 10
16
+ z = 3
17
+
18
+ # using kwargs
19
+ print f (x = x , y = y , z = z )
20
+
21
+ # using args and kwargs
22
+ print f (y , x , z = 2 )
23
+
24
+ # using args and kwargs out of order
25
+ print f (z = 3 , y = x , x = y )
26
+
27
+ # all x
28
+ print f (z = x , y = x , x = x )
29
+
30
+ """
31
+ With defaults
32
+ """
33
+
34
+ def ff (x = 20 , y = 10 , z = 3 ):
35
+ # uncomment for debug
36
+ # print "x=%s y=%s, z=%s" % (x, y, z)
37
+ return (x + y ) / float (z )
38
+
39
+ print ff (10 , 5 , 3 )
40
+
41
+ # using args
42
+ print ff (10 )
43
+
44
+ # using kwargs
45
+ print ff (z = 10 )
46
+
47
+ x = 20
48
+ print ff (z = x , y = x )
49
+
50
+
You can’t perform that action at this time.
0 commit comments