blob: 1c0c523d6858366bdefdbe843fecd5f20c7e3184 [file] [log] [blame]
Johannes Gijsbers39815112004-09-24 21:36:52 +00001doctests = """
Barry Warsaw09f95471997-08-25 22:17:45 +00002
Johannes Gijsbers39815112004-09-24 21:36:52 +00003Unpack tuple
Barry Warsaw09f95471997-08-25 22:17:45 +00004
Johannes Gijsbers39815112004-09-24 21:36:52 +00005 >>> t = (1, 2, 3)
6 >>> a, b, c = t
7 >>> a == 1 and b == 2 and c == 3
8 True
Barry Warsaw09f95471997-08-25 22:17:45 +00009
Johannes Gijsbers39815112004-09-24 21:36:52 +000010Unpack list
Barry Warsaw09f95471997-08-25 22:17:45 +000011
Johannes Gijsbers39815112004-09-24 21:36:52 +000012 >>> l = [4, 5, 6]
13 >>> a, b, c = l
14 >>> a == 4 and b == 5 and c == 6
15 True
Barry Warsaw09f95471997-08-25 22:17:45 +000016
Johannes Gijsbers39815112004-09-24 21:36:52 +000017Unpack implied tuple
Barry Warsaw09f95471997-08-25 22:17:45 +000018
Johannes Gijsbers39815112004-09-24 21:36:52 +000019 >>> a, b, c = 7, 8, 9
20 >>> a == 7 and b == 8 and c == 9
21 True
Barry Warsaw09f95471997-08-25 22:17:45 +000022
Johannes Gijsbers39815112004-09-24 21:36:52 +000023Unpack string... fun!
Barry Warsaw09f95471997-08-25 22:17:45 +000024
Johannes Gijsbers39815112004-09-24 21:36:52 +000025 >>> a, b, c = 'one'
26 >>> a == 'o' and b == 'n' and c == 'e'
27 True
Barry Warsaw09f95471997-08-25 22:17:45 +000028
Johannes Gijsbers39815112004-09-24 21:36:52 +000029Unpack generic sequence
Barry Warsaw0157e7a2000-10-12 14:45:58 +000030
Johannes Gijsbers39815112004-09-24 21:36:52 +000031 >>> class Seq:
32 ... def __getitem__(self, i):
33 ... if i >= 0 and i < 3: return i
34 ... raise IndexError
35 ...
36 >>> a, b, c = Seq()
37 >>> a == 0 and b == 1 and c == 2
38 True
Barry Warsaw09f95471997-08-25 22:17:45 +000039
Johannes Gijsbers39815112004-09-24 21:36:52 +000040Single element unpacking, with extra syntax
Barry Warsaw09f95471997-08-25 22:17:45 +000041
Johannes Gijsbers39815112004-09-24 21:36:52 +000042 >>> st = (99,)
43 >>> sl = [100]
44 >>> a, = st
45 >>> a
46 99
47 >>> b, = sl
48 >>> b
49 100
Barry Warsaw09f95471997-08-25 22:17:45 +000050
Johannes Gijsbers39815112004-09-24 21:36:52 +000051Now for some failures
Barry Warsaw09f95471997-08-25 22:17:45 +000052
Johannes Gijsbers39815112004-09-24 21:36:52 +000053Unpacking non-sequence
Barry Warsaw09f95471997-08-25 22:17:45 +000054
Johannes Gijsbers39815112004-09-24 21:36:52 +000055 >>> a, b, c = 7
56 Traceback (most recent call last):
57 ...
Serhiy Storchaka13a6c092017-12-26 12:30:41 +020058 TypeError: cannot unpack non-iterable int object
Barry Warsaw09f95471997-08-25 22:17:45 +000059
Johannes Gijsbers39815112004-09-24 21:36:52 +000060Unpacking tuple of wrong size
Barry Warsaw09f95471997-08-25 22:17:45 +000061
Johannes Gijsbers39815112004-09-24 21:36:52 +000062 >>> a, b = t
63 Traceback (most recent call last):
64 ...
Georg Brandl0310a832010-07-10 10:32:36 +000065 ValueError: too many values to unpack (expected 2)
Barry Warsaw09f95471997-08-25 22:17:45 +000066
Johannes Gijsbers39815112004-09-24 21:36:52 +000067Unpacking tuple of wrong size
Barry Warsaw09f95471997-08-25 22:17:45 +000068
Johannes Gijsbers39815112004-09-24 21:36:52 +000069 >>> a, b = l
70 Traceback (most recent call last):
71 ...
Georg Brandl0310a832010-07-10 10:32:36 +000072 ValueError: too many values to unpack (expected 2)
Barry Warsaw09f95471997-08-25 22:17:45 +000073
Johannes Gijsbers39815112004-09-24 21:36:52 +000074Unpacking sequence too short
Barry Warsaw09f95471997-08-25 22:17:45 +000075
Johannes Gijsbers39815112004-09-24 21:36:52 +000076 >>> a, b, c, d = Seq()
77 Traceback (most recent call last):
78 ...
R David Murray4171bbe2015-04-15 17:08:45 -040079 ValueError: not enough values to unpack (expected 4, got 3)
Barry Warsaw09f95471997-08-25 22:17:45 +000080
Johannes Gijsbers39815112004-09-24 21:36:52 +000081Unpacking sequence too long
Barry Warsaw09f95471997-08-25 22:17:45 +000082
Johannes Gijsbers39815112004-09-24 21:36:52 +000083 >>> a, b = Seq()
84 Traceback (most recent call last):
85 ...
Georg Brandl0310a832010-07-10 10:32:36 +000086 ValueError: too many values to unpack (expected 2)
Barry Warsaw09f95471997-08-25 22:17:45 +000087
Johannes Gijsbers39815112004-09-24 21:36:52 +000088Unpacking a sequence where the test for too long raises a different kind of
89error
90
91 >>> class BozoError(Exception):
92 ... pass
93 ...
94 >>> class BadSeq:
95 ... def __getitem__(self, i):
96 ... if i >= 0 and i < 3:
97 ... return i
98 ... elif i == 3:
99 ... raise BozoError
100 ... else:
101 ... raise IndexError
102 ...
103
104Trigger code while not expecting an IndexError (unpack sequence too long, wrong
105error)
106
107 >>> a, b, c, d, e = BadSeq()
108 Traceback (most recent call last):
109 ...
Guido van Rossum6a2a2a02006-08-26 20:37:44 +0000110 test.test_unpack.BozoError
Johannes Gijsbers39815112004-09-24 21:36:52 +0000111
112Trigger code while expecting an IndexError (unpack sequence too short, wrong
113error)
114
115 >>> a, b, c = BadSeq()
116 Traceback (most recent call last):
117 ...
Guido van Rossum6a2a2a02006-08-26 20:37:44 +0000118 test.test_unpack.BozoError
Johannes Gijsbers39815112004-09-24 21:36:52 +0000119
Berker Peksag094c9c92016-05-18 08:44:29 +0300120Allow unpacking empty iterables
121
122 >>> () = []
123 >>> [] = ()
124 >>> [] = []
125 >>> () = ()
126
127Unpacking non-iterables should raise TypeError
128
129 >>> () = 42
130 Traceback (most recent call last):
131 ...
Serhiy Storchaka13a6c092017-12-26 12:30:41 +0200132 TypeError: cannot unpack non-iterable int object
Berker Peksag094c9c92016-05-18 08:44:29 +0300133
134Unpacking to an empty iterable should raise ValueError
135
136 >>> () = [42]
137 Traceback (most recent call last):
138 ...
139 ValueError: too many values to unpack (expected 0)
140
Johannes Gijsbers39815112004-09-24 21:36:52 +0000141"""
142
143__test__ = {'doctests' : doctests}
144
145def test_main(verbose=False):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000146 from test import support
Johannes Gijsbers39815112004-09-24 21:36:52 +0000147 from test import test_unpack
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000148 support.run_doctest(test_unpack, verbose)
Johannes Gijsbers39815112004-09-24 21:36:52 +0000149
150if __name__ == "__main__":
151 test_main(verbose=True)