3 ms·
That last one is not idiomatic (I’ll ignore the lowercase first letter of True and False). The `else` there is superfluous; that cuts it down to this: def
by chrismorgan 12y ago
That last one is not idiomatic (I’ll ignore the lowercase first letter of True and False).
The `else` there is superfluous; that cuts it down to this:
def look_for_something(parameter):
for item in list:
if matches(item, parameter):
return True
return False
And then after that one should just replace the entire loop with an `any` call:
def look_for_something(parameter):
return any(matches(item, parameter) for item in list)
Also, if we assume a `matches()` that is simple equality, then it would just be
def look_for_something(parameter):
return parameter in list
… and even then, you shouldn’t have named a variable `list`.
Cut down to its essence like this, the function probably shouldn’t have even existed… ☺
- blahedo 12y agoRemember that I was talking about novice programmers, here. I'll concede that "idiomatic" was a bit strong (and the True/False thing was a brain fart), but I think my main point stands: encouraging the use of an "else:" that can attach to loops, with an unobvious and somewhat nuanced semantics, is not kind to newcomers. And the fact remains that the difference between a correct implementation and one that is wrong in exactly the way that a lot of newcomers get it wrong is nothing but indentation.