中级农民
- 积分
- 165
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2021-11-23
- 最后登录
- 1970-1-1
|
node1 = TrieNode()
node2 = TrieNode()
node1.is_word = True
print(node2.is_word)
是True还是False?
首先要理解reference-
Object 本身不需要有名字。我们只需要有一种方式能够找到它(reference)。
(1)用variable name
node1 = TrieNode()
node2 = TrieNode()
这里 node1 和 node2 是variable name,分别指向两个不同的 objects。 我们可以直接访问它们的 attributes:
node1.children
node1.is_word
node2.children
node2.is_word
(2)通过 HashMap 的 key 访问
root = TrieNode()
# 这两行代码是在 创建两个新的 TrieNode 对象,并把它们添加到 root 的 children hashmap 中。
root.children["a"] = TrieNode()
root.children["b"] = TrieNode()
怎么访问?
root.children["a"] # 获取第一个子节点对象
root.children["b"] # 获取第二个子节点对象 |
|