![]() |
difference between != and is not - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: difference between != and is not (/thread-41150.html) |
difference between != and is not - akbarza - Nov-18-2023 hi I wrote the below code in vs code and it took an error in line 7 but in idle the code ran okay: '''a test for regex ''' import re REGEX = r"(?im:foo),\w+" m = re.search(REGEX, 'FOo,bar_baz') if m is not None: # i first had written m != None, # Comparison 'm != None' should be 'm is not None' print(f'm.group(0): {m.group(0)}') else: print(f'result of match is: {m}')in line 7 as you can see, I first wrote m!==Noneand vs code took an error and suggested to me to change to m is not None. what is the difference between !== and is not? thanks RE: difference between != and is not - deanhystad - Nov-18-2023 None is a singleton. There is only one None object. is/is not None checks if an object is the None object or not. Checking ihe value, which is what != does, is superfluous. No object other than None wiill == None. In addition to None, True and False are singltons and you should never test ==True or == False, but rather is True or is False. Well, you should not do that either unless you really want to know if something is the True object or the False object. |