For review: PEP 308 - If-then-else expression

Andrew Koenig ark at research.att.com
Mon Feb 10 14:00:52 EST 2003


Here's another example, this one from Lib/test/test_iter.py, starting
at line 390:

    # Test map()'s use of iterators.
    def test_builtin_map(self):
        self.assertEqual(map(None, SequenceClass(5)), range(5))
        self.assertEqual(map(lambda x: x+1, SequenceClass(5)), range(1, 6))

        d = {"one": 1, "two": 2, "three": 3}
        self.assertEqual(map(None, d), d.keys())
        self.assertEqual(map(lambda k, d=d: (k, d[k]), d), d.items())
        dkeys = d.keys()
        expected = [(i < len(d) and dkeys[i] or None,
                     i,
                     i < len(d) and dkeys[i] or None)
                    for i in range(5)]

and so on.

This code works as written, but only by coincidence.  If, for example,
I were to change the initialization of d to

        d = {"": 0, "one": 1, "two": 2, "three": 3}

then the code would break.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list