Skip to content

Commit e0ba749

Browse files
authored
Update 0208-implement-trie-prefix-tree.py
It is better to use is None instead of == None when checking for None in Python. The reason is that `is` checks for identity (whether the two objects are the same object in memory), whereas == checks for equality (whether the two objects have the same value). In Python, None is a singleton object, and using is to check for None is more efficient and idiomatic. It ensures that you are specifically checking if a variable is bound to the None object and not just a value that happens to be equivalent to None.
1 parent d08a520 commit e0ba749

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

python/0208-implement-trie-prefix-tree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def insert(self, word: str) -> None:
1818
curr = self.root
1919
for c in word:
2020
i = ord(c) - ord("a")
21-
if curr.children[i] == None:
21+
if curr.children[i] is None:
2222
curr.children[i] = TrieNode()
2323
curr = curr.children[i]
2424
curr.end = True
@@ -30,7 +30,7 @@ def search(self, word: str) -> bool:
3030
curr = self.root
3131
for c in word:
3232
i = ord(c) - ord("a")
33-
if curr.children[i] == None:
33+
if curr.children[i] is None:
3434
return False
3535
curr = curr.children[i]
3636
return curr.end
@@ -42,7 +42,7 @@ def startsWith(self, prefix: str) -> bool:
4242
curr = self.root
4343
for c in prefix:
4444
i = ord(c) - ord("a")
45-
if curr.children[i] == None:
45+
if curr.children[i] is None:
4646
return False
4747
curr = curr.children[i]
4848
return True

0 commit comments

Comments
 (0)