[Python-projects] Spurious warning about possibly unused loop var

skip at pobox.com skip at pobox.com
Sun Nov 23 04:02:07 CET 2008


Clipped almost byte-for-byte from the SpamBayes source code:

    def f(self, v0, v1):
        for sep in [' ', ',', ':', ';', '/', '\\', None]:
            # we know at this point that len(self.value) is at
            # least two, because len==0 and len==1 were dealt
            # with as special cases
            test_str = str(v0) + sep + str(v1)
            test_tuple = self.split_values(test_str)
            if test_tuple[0] == str(v0) and \
               test_tuple[1] == str(v1) and \
               len(test_tuple) == 2:
                break
        # cache this so we don't always need to do the above
        self.delimiter = sep

Pylint (0.15.2, astng 0.17.4, common 0.36.1) complains about the assignment
after the for loop:

    loopvar.py:13: [W, f] Using possibly undefined loop variable 'sep'

Given that the list is a non-empty list of literals I think it's safe to
assume that sep will have a value when the loop completes.

-- 
Skip Montanaro - skip at pobox.com - http://smontanaro.dyndns.org/


More information about the Python-Projects mailing list