2

I was practicing LeetCode and got confused on the scope of self for a solution. I thought that self is a local variable to diameterOfBinaryTree(), so the nested function height() should only be able to read self.ans and not modify it, according to LEGB scoping rule. So how can height() modify self.ans in this solution? Thanks!

P.S. I couldn't get "class Solution(object):" to format correctly so just assume that the indentation is correct.

class Solution(object):
def diameterOfBinaryTree(self, root):

    self.ans = 0
    
    def height(p):
        if not p: return -1       
                        
        left, right = height(p.left), height(p.right)
        
        self.ans = max(self.ans, 2+left+right)   
        
        return 1+max(left, right)
        
    height(root)
    return self.ans
1
  • 2
    The LEGB rule is about variables, not attributes. self is not being assigned a different value. It is the attribute that is assigned a different value. Commented Aug 1, 2022 at 21:07

1 Answer 1

2

Most of your reasoning is correct, except for one thing: self.ans = ... is not rebinding anything in the non-local scope. Instead, the .= operator is syntactic sugar for setattr(self, 'ans', ...). Clearly the function call does not violate LEGB in any way, since the reference to self is read-only.

For perhaps an easier example, consider the difference between some_list.extend([1, 2, 3]) vs some_list += [1, 2, 3]. The former is a method call that happens to modify a mutable object, while the latter attempts to reassign the reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.