简体   繁体   English

如何在 def 中使用全局变量?

[英]how can I use a global variable in the def?

Like so;像这样; the error is num is not defined错误是num未定义

I want to use backtracking but how can I use a global variable in the funcation?我想使用回溯,但如何在函数中使用全局变量?

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        num = n
        def huisu(head):
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0:
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        huisu(head)

num is out of scope. num超出 scope。 You have to define it before your inner function:您必须在您的内部 function 之前定义它:

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        num = 0 # or some other default value
        def huisu(head):
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0:
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        num = n
        huisu(head)

You are tring to evaluate something which is not declared before... How can i know if num is equal to 0 if you didn't even declare it?您正在尝试评估之前未声明的内容...如果您甚至没有声明,我怎么知道 num 是否等于 0?

Probably you missed a portion of function?可能你错过了 function 的一部分? By the way, the best solution is to declare it outside the def statment so the variable may be visible at the whole function.顺便说一句,最好的解决方案是在 def 语句之外声明它,这样变量就可以在整个 function 中可见。

Also remember to assign to it the correct value, num is in fact never used还记得给它分配正确的值,实际上从未使用过 num

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        
        def huisu(head):
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0: # here
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        num = n
        huisu(head)

Did you mean:你的意思:

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        num = n # Added
        def huisu(head):
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0: # here
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        num = n
        huisu(head)

Python has three types of variable scopes: Python 具有三种类型的变量作用域:

  • Globals: variables defined outside a function body全局变量:在 function 主体之外定义的变量
  • local: variables defined within a function body local:在 function 体内定义的变量
  • nonlocal: variables defined within a nested function nonlocal:在嵌套的 function 中定义的变量

The scope is determined by where the variable is assigned, not by where it is used (read). scope 取决于变量的分配位置,而不是使用(读取)的位置。

Function huisu has an assignment of num, so the variable is considered local to function huisu ie the line: Function huisu的赋值为 num,因此该变量被认为是 function huisu 的局部变量,即:

num = num - 1

Since it has not previously been given a value, the error occurs.由于之前没有给它赋值,所以会发生错误。

Two solutions两种解决方案

Solution 1解决方案 1

Declare num as nonlocal将 num 声明为非本地

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        num = n             # Could assign num here

        def huisu(head):
            nonlocal num    # this declaration uses num in outer function
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0:
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        num = n             # Or here, doesn't matter
                            # What matters is assigned before function call
        huisu(head)

Solution 2解决方案 2

Pass num as a parameter (more typical solution)将 num 作为参数传递(更典型的解决方案)

class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        
        def huisu(head, num):  # pass num as 2nd argument
         
            if head == None:
                return
            else:
                before = head
                head = head.next
                huisu(head)
                if num == 0:
                    head.next = head.next.next
                else:
                    num = num - 1
                head = before

            return head
        
        huisu(head, n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM