I was trying to solve a dynamic programming problem in Pyhon 3.x. I wanted to create a memo dictionary object. After assigning default value of None to memo parameter, the check for None with not object doesn't seem to work, but if I change it to object is None, it works fine.
By doesn't work, I mean this condition doesn't evaluate to True in case of not object, and the following indented code isn't executed.
This results in TLE on LeetCode. But using is None check does work, and it assigns a dictionary to memo, and thus caching works as intended, and the solution passes.
Why does this happen?
def lcs(i, j, memo = None):
if not memo:
memo = {}
vs
def lcs(i, j, memo = None):
if memo is None:
memo = {}
I called the function with return lcs(0, 0).
Edit: On my system Python IDLE, both expressions evaluate to True. So I think, the LeetCode platform handles the code differently.
CodePudding user response:
not is a keyword which is equivalent to !=. So it has totally different meaning.
You can use not keyword as below (for example)
def lcs(i, j, memo = None):
if memo is not None:
memo = {}
The above code is equivalent to
def lcs(i, j, memo = None):
if memo != None:
memo = {}
If you want to use not keyword that badly, then use this code.
def lcs(i, j, memo = None):
if not memo == None:
memo = {}
If you find have any doubts, let me know.
CodePudding user response:
From Truth Value Testing
By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero.
An empty dict has length zero, so is False. If you call lcs with a memo dictionary, but that dictionary doesn't happen to have anything in it, it will test False and if not memo: memo = {} will create a new memo object for you. That code can't tell the difference between an empty memo and None.
The if memo is None is the proper way to assign a memo only if the caller hasn't passed one in.
CodePudding user response:
It's been awhile since I've used Python so bear with me but I think I can explain the general programming concept.
In the first one you are checking if memo doesn't exist.
if not memo
Meaning to continue if memo does not exist.
In the second one you are checking that memo does exist and that it is set to None.
if memo is None
To continue if memo exists (a pre-condition to going into the check) and it is set to None
CodePudding user response:
Works fine for me. Maybe explain what "doesn't work" mean. They both yield the same results.

