Is there a substantial difference in Python 3.x between:
for each_line in data_file:
if each_line.find(":") != -1:
#placeholder for code
#more placeholder
and
for each_line in data:
if not each_line.find(":") == -1:
#placeholder for code
#more placeholder
My question isn’t particular to the above usage, but is more general or essential – is this syntactical difference working in a different way, even though the result is the same? Is there a logical difference? Are there tasks where one is more appropriate or is this solely a stylistic difference? If this is merely stylistic, which one is considered cleaner by Python programmers?
Also, is the above an opposite instance of asking what the difference is between is and ==? Is the former, like the latter, a difference of object identity and object value equality? What I mean is, in my above example, is the is in using not implicit?
As I understand it,
functionally they are not entirely the same; if you are comparing against a class, the class could have a member function,
__ne__which is called when using the comparison operator !=, as opposed to__eq__which is called when using the comparison ==So, in this instance,
not (a == b)would call__eq__on a, with b as the argument, thennotthe result(a != b)would call__ne__on a, with b as the argument.I would use the first method (using !=) for comparison