<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">doctests = """

Unpack tuple

    &gt;&gt;&gt; t = (1, 2, 3)
    &gt;&gt;&gt; a, b, c = t
    &gt;&gt;&gt; a == 1 and b == 2 and c == 3
    True

Unpack list

    &gt;&gt;&gt; l = [4, 5, 6]
    &gt;&gt;&gt; a, b, c = l
    &gt;&gt;&gt; a == 4 and b == 5 and c == 6
    True

Unpack implied tuple

    &gt;&gt;&gt; a, b, c = 7, 8, 9
    &gt;&gt;&gt; a == 7 and b == 8 and c == 9
    True

Unpack string... fun!

    &gt;&gt;&gt; a, b, c = 'one'
    &gt;&gt;&gt; a == 'o' and b == 'n' and c == 'e'
    True

Unpack generic sequence

    &gt;&gt;&gt; class Seq:
    ...     def __getitem__(self, i):
    ...         if i &gt;= 0 and i &lt; 3: return i
    ...         raise IndexError
    ...
    &gt;&gt;&gt; a, b, c = Seq()
    &gt;&gt;&gt; a == 0 and b == 1 and c == 2
    True

Single element unpacking, with extra syntax

    &gt;&gt;&gt; st = (99,)
    &gt;&gt;&gt; sl = [100]
    &gt;&gt;&gt; a, = st
    &gt;&gt;&gt; a
    99
    &gt;&gt;&gt; b, = sl
    &gt;&gt;&gt; b
    100

Now for some failures

Unpacking non-sequence

    &gt;&gt;&gt; a, b, c = 7
    Traceback (most recent call last):
      ...
    TypeError: cannot unpack non-iterable int object

Unpacking tuple of wrong size

    &gt;&gt;&gt; a, b = t
    Traceback (most recent call last):
      ...
    ValueError: too many values to unpack (expected 2)

Unpacking tuple of wrong size

    &gt;&gt;&gt; a, b = l
    Traceback (most recent call last):
      ...
    ValueError: too many values to unpack (expected 2)

Unpacking sequence too short

    &gt;&gt;&gt; a, b, c, d = Seq()
    Traceback (most recent call last):
      ...
    ValueError: not enough values to unpack (expected 4, got 3)

Unpacking sequence too long

    &gt;&gt;&gt; a, b = Seq()
    Traceback (most recent call last):
      ...
    ValueError: too many values to unpack (expected 2)

Unpacking a sequence where the test for too long raises a different kind of
error

    &gt;&gt;&gt; class BozoError(Exception):
    ...     pass
    ...
    &gt;&gt;&gt; class BadSeq:
    ...     def __getitem__(self, i):
    ...         if i &gt;= 0 and i &lt; 3:
    ...             return i
    ...         elif i == 3:
    ...             raise BozoError
    ...         else:
    ...             raise IndexError
    ...

Trigger code while not expecting an IndexError (unpack sequence too long, wrong
error)

    &gt;&gt;&gt; a, b, c, d, e = BadSeq()
    Traceback (most recent call last):
      ...
    test.test_unpack.BozoError

Trigger code while expecting an IndexError (unpack sequence too short, wrong
error)

    &gt;&gt;&gt; a, b, c = BadSeq()
    Traceback (most recent call last):
      ...
    test.test_unpack.BozoError

Allow unpacking empty iterables

    &gt;&gt;&gt; () = []
    &gt;&gt;&gt; [] = ()
    &gt;&gt;&gt; [] = []
    &gt;&gt;&gt; () = ()

Unpacking non-iterables should raise TypeError

    &gt;&gt;&gt; () = 42
    Traceback (most recent call last):
      ...
    TypeError: cannot unpack non-iterable int object

Unpacking to an empty iterable should raise ValueError

    &gt;&gt;&gt; () = [42]
    Traceback (most recent call last):
      ...
    ValueError: too many values to unpack (expected 0)

"""

__test__ = {'doctests' : doctests}

def test_main(verbose=False):
    from test import support
    from test import test_unpack
    support.run_doctest(test_unpack, verbose)

if __name__ == "__main__":
    test_main(verbose=True)
</pre></body></html>