[Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

Peter Otten __peter__ at web.de
Sun Jan 17 05:52:07 EST 2016


boB Stepp wrote:

>> The hard part is to remember to test whenever a negative index is
>> calculated.
> 
> I am assuming that this is relevant to what just came before, the use
> of this "or None" check.  Is this correct?

No, I mean that you always should test your code against the corner cases. 
For example a trivial and seemingly harmless function

def tail(items, size):
    return items[-size:]

should return an empty list with size=0 (you might get away with undefined 
behaviour for size<0). If you only have a test

class T(unittest.TestCase):
    def test_tail(self):
        self.assertEqual(tail("abcde", 2), "de")

your coverage tool might be happy, but you are still in for trouble. You 
need at least

self.assertEqual(tail("abcde", 0), "")

to be prepared for the 0 == -0 problem.



More information about the Tutor mailing list