从我读到的here,子作用域应该可以访问父作用域中定义的变量。但是,在我的例子中,count
上有一个未解决的错误。为什么会这样
def find_kth_largest_bst(root, k):
count = 0
def _find_kth_largest_bst(root, k):
if not root:
return None
_find_kth_largest_bst(root.right, k)
count += 1 #unresolved error here??
pass
Tags:
您可以使用
nonlocal
关键字从父作用域访问变量您所做的是使用内部函数,这与类继承不同。另一个非常类似的假设是:
Python nested functions variable scoping
这个问题的答案是:
这意味着您可以用
global
或nonlocal
语句来解决错误这里的另一件事是
count = 0
有双制表符或8个空格,而它应该只有一个相关问题 更多 >
编程相关推荐