I was wondering what type of optimizations are done automatically by the python interpreter.
E.g. I have the following code to check whether a string starts with any substrings in startList:
def startswithAny(string, startList):
return any([string.startswith(x) for x in startList])
Let's assume startList contains 1000 entries, but string.startswith(startList[0]) already yields true. What will happen? Will [string.startswith(x) for x in startList] still be fully evaluated? Or will the interpreter recognize that any() yields true already?
If not, than IMHO it makes more sense to write codes like these non-pythonic, like this:
def startswithAny(string, startList):
for x in startList:
if string.startswith(x):
return True
return False
Thanks