Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 97 lines (82 sloc) 3.646 kb
6b30a1f Glenn Downing another commit
authored
1 #!/usr/bin/env python3
2
3 # ----------------
4 # FunctionTuple.py
5 # ----------------
6
7 print("FunctionTuple.py")
8
9 def f (x, y, *z) :
10 assert type(z) is tuple
11 return [x, y, z]
12
13 def g (x, y, *z) :
14 assert type(z) is tuple
15 return [x, y, set(z)]
16
17 assert f(2, 3) == [2, 3, ()]
18 assert f(2, 3, 4) == [2, 3, (4,)]
19 assert f(2, 3, 4, 5) == [2, 3, (4, 5)]
20
21 t = (3, 4)
22 u = (2,)
23 assert t == (3, 4)
24 assert t != (4, 3)
25 assert f(2, t, 5) == [2, (3, 4), (5,)]
26 assert f(2, 5, t) == [2, 5, ((3, 4),)]
27 assert f(2, 5, *t) == [2, 5, (3, 4)]
28 assert f(2, *t) == [2, 3, (4,)]
29 assert f(*t) == [3, 4, ()]
30 assert f(y = 3, *u) == [2, 3, ()]
31 assert f(*u, y = 3) == [2, 3, ()]
32 #f(2, y = 5, *t) # TypeError: f() got multiple values for argument 'y'
33 #f(x = 2, y = 5, *t) # TypeError: f() got multiple values for argument 'x'
34
35 l = [3, 4]
36 u = [2]
37 assert l == [3, 4]
38 assert l != [4, 3]
39 assert f(2, l, 5) == [2, [3, 4], (5,)]
40 assert f(2, 5, l) == [2, 5, ([3, 4],)]
41 assert f(2, 5, *l) == [2, 5, (3, 4)]
42 assert f(2, *l) == [2, 3, (4,)]
43 assert f(y = 3, *u) == [2, 3, ()]
44 assert f(*u, y = 3) == [2, 3, ()]
45 #f(2, y = 5, *l) # TypeError: f() got multiple values for argument 'y'
46 #f(x = 2, y = 5, *l) # TypeError: f() got multiple values for argument 'x'
47
48 s = {3, 4}
49 u = {2}
50 assert s == {4, 3}
51 assert s == {3, 4}
52 assert f(2, s, 5) == [2, {3, 4}, (5,)]
53 assert f(2, 5, s) == [2, 5, ({3, 4},)]
54 assert g(2, 5, *s) == [2, 5, {3, 4}]
55 assert g(2, *s) == [2, 3, {4}] # ?
56 assert g(y = 3, *u) == [2, 3, set()]
57 assert g(*u, y = 3) == [2, 3, set()]
58 #g(2, y = 5, *s) # TypeError: f() got multiple values for argument 'y'
59 #g(x = 2, y = 5, *s) # TypeError: f() got multiple values for argument 'x'
60
61 d = {"b" : 4, "a" : 3}
62 u = {2 : "c"}
63 assert d == {'b' : 4, 'a' : 3}
64 assert d == {'a' : 3, 'b' : 4}
65 assert f(2, d, 5) == [2, {'a' : 3, 'b' : 4}, (5,)]
66 assert f(2, 5, d) == [2, 5, ({'a' : 3, 'b' : 4},)]
67 assert g(2, 5, *d.keys()) == [2, 5, {'a', 'b'}]
68 assert g(2, 5, *d.values()) == [2, 5, {3, 4}]
69 assert g(2, 5, *d.items()) == [2, 5, {('a', 3), ('b', 4)}]
70 assert g(2, 5, *d) == [2, 5, {'a', 'b'}]
71 assert g(2, *d) == [2, 'a', {'b'}] # ?
72 assert g(y = 3, *u) == [2, 3, set()]
73 assert g(*u, y = 3) == [2, 3, set()]
74 #f(2, y = 5, *d) # TypeError: f() got multiple values for argument 'y'
75 #f(x = 2, y = 5, *d) # TypeError: f() got multiple values for argument 'x'
76 #f(**d) # TypeError: f() got an unexpected keyword argument 'a'
77
78 d = {"z" : 4, "y" : 3}
79 #f(2, **d) # TypeError: f() got an unexpected keyword argument 'z'
80 #f(**d) # TypeError: f() got an unexpected keyword argument 'z'
81
82 d = {"y" : 3, "x" : 2}
83 #f(2, **d) # TypeError: f() got multiple values for keyword argument 'x'
84 assert f(**d) == [2, 3, ()]
85
86 d = {"y" : 3}
87 assert f(2, **d) == [2, 3, ()]
88 assert f(x = 2, **d) == [2, 3, ()]
89 #f(**d) # TypeError: f() takes at least 2 arguments (1 given)
90
91 d = {"y" : 3, "t" : 5}
92 #f(2, **d) # TypeError: f() got an unexpected keyword argument 't'
93 #f(x = 2, **d) # TypeError: f() got an unexpected keyword argument 't'
94 #f(**d) # TypeError: f() got an unexpected keyword argument 't'
95
96 print("Done.")
Something went wrong with that request. Please try again.