blob: 75033ed4805fbba8d95fa4d7a607d82d1c6c1266 [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 ...
Guido van Rossumd8faa362007-04-27 19:54:29 +000058 TypeError: 'int' object is not iterable
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 ...
65 ValueError: too many values to unpack
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 ...
72 ValueError: too many values to unpack
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 ...
79 ValueError: need more than 3 values to unpack
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 ...
86 ValueError: too many values to unpack
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
120"""
121
122__test__ = {'doctests' : doctests}
123
124def test_main(verbose=False):
125 import sys
126 from test import test_support
127 from test import test_unpack
128 test_support.run_doctest(test_unpack, verbose)
129
130if __name__ == "__main__":
131 test_main(verbose=True)