简体   繁体   中英

Usage of "self" in Python leetcode problem

I was working on 109. Convert Sorted List to Binary Search Tree on leetcode, and I came across a solution that I mostly understand, aside from the use of self.

The solution:

class Solution:
    def sortedListToBST(self, head):
        length = 0
        curr = head
        while curr:
            curr = curr.next
            length += 1

        self.head = head

        def recursion(start, end):
            if start > end: return None
            middle = (start + end) // 2
            # left
            left = recursion(start, middle - 1)

            # root
            root = TreeNode(self.head.val)
            self.head = self.head.next
            root.left = left

            # right
            root.right = recursion(middle + 1, end)
            return root

        return recursion(0, length - 1)

I get confused on the use of self.head = head . I understand self is used to indicate or specify the current instance of a class, and is used to access variables of said class. My current understanding of how this is working is that self.head is being defined as a global variable (outside the scope of recursion(start,end) ) that points to the object head . I don't understand why self needs to be used, and why we can't just say something like copyOfHead = head instead of self.copyOfHead = head . I'm sure I'm getting some things wrong here - can somebody help me better understand the what and why of using self. in this instance?

It's just providing a global variable so that recursion (which will be called many times) can use it to store state between calls.

Leetcode is designed so that you have to use the class wrapper, but for this algorithm you could have easily written:

self_head = None

def sortedListToBST(head):
    length = 0
    curr = head
    while curr:
        curr = curr.next
        length += 1

    self_head = head

    return recursion(0, length - 1)

def recursion(start, end):
    if start > end: return None
    middle = (start + end) // 2
    # left
    left = recursion(start, middle - 1)

    # root
    root = TreeNode(self_head.val)
    self_head = self_head.next
    root.left = left

    # right
    root.right = recursion(middle + 1, end)
    return root

# Run the function
sortedListToBST(some_input)

Which I think makes it clear that the object-oriented stuff doesn't matter, it's more about being a global variable. However, in your example, recursion is inside sortedListToBST , so I think it could have actually accessed head already. So I think using self was unnecessary and the author didn't realize it. In functional programming terms self.head allows recursion to have side-effects.

You could also eliminate the need for this global variable by just passing head in the arguments of recursion .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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