Is there a logical difference between 'not ==' and '!= (without is)

后端 未结 3 566
太阳男子
太阳男子 2020-12-20 23:49

Is there a substantial difference in Python 3.x between:

for each_line in data_file:
    if each_line.find(\":\") != -1:
        #placeholder for code
               


        
3条回答
  •  一整个雨季
    2020-12-21 00:37

    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, then not the result
    (a != b) would call __ne__ on a, with b as the argument.

    I would use the first method (using !=) for comparison

提交回复
热议问题