Search code examples
pythonshort-circuiting

How can this behavior be acomplished? Python "short circuting" test


I have the following code:

def testGeodatabase(self):
    geodatabaseList = self.gp.ListWorkspaces("*","ALL")
    for x in geodatabaseList:
        if x == self.outputGeodatabase:
            return True
        else:
            pass
    return False

What i need to know the following: in case the if condition evaluates to true, will the function stop looking in the list and never return False? Or do i need a break statement?

def testGeodatabase(self):
    geodatabaseList = self.gp.ListWorkspaces("*","ALL")
    for x in geodatabaseList:
        if x == self.outputGeodatabase:
            return True
            break
        else:
            pass
    return False

If the following code does not solve my problem, what can i use to do simulate that behavior?

Thanks


Solution

  • return is the end of the line, and nothing else will happen in that function afterwards. On the other hand, you could rewrite your function as

    def testGeodatabase(self):
        return self.outputGeodatabase in self.gp.ListWorkspaces("*","ALL")