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

Tim Peters tim.one at comcast.net
Mon Feb 10 14:30:38 EST 2003


[Andrew Koenig]
> 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.

You're pushing this too hard.  Pointing out examples of code that work fine,
but wouldn't *if* you changed them, isn't a worthy exercise.

> 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.

That's why d wasn't initialized that way (I wrote this test, so I happen to
know that; the test isn't a random collection of independent lines).






More information about the Python-list mailing list