【发布时间】:2023-04-09 03:48:01
【问题描述】:
在练习算法和数据结构的同时,我正在学习使用 mypy 和静态类型检查器。
在二叉搜索树中,节点被初始化为没有子节点。它们是节点类型。但是,似乎 None 在 Python 中是它自己的对象类型,所以 mypy 在下面给了我一个错误。是否可以将未分配的孩子初始化为类型节点?
binary_search_tree.py:17: error: Incompatible types in assignment (expression has type "None", variable has type "Node")
binary_search_tree.py:18: error: Incompatible types in assignment (expression has type "None", variable has type "Node")
Found 2 errors in 1 file (checked 1 source file)
代码如下:
class Node:
# A node has a value which is an int and two children which are nodes
def __init__(self, value: int):
self.value: int = value
self.left: Node = None
self.right: Node = None
【问题讨论】:
标签: python python-3.x types mypy dynamic-typing