title: Clarification on local and global variable scope in Python
body: <p>I'm a bit confused regarding local and global variables referenced outside a function scope. For one problem, I had this code and it worked:</p>
  <pre><code>def leafSimilar(self, root1: TreeNode, root2: TreeNode) -&gt; bool:
      l1 = []
      l2 = []
          
      def traverseLeaf(root: TreeNode, sequence: list): 
          if not root: return 
          elif not root.left and not root.right: sequence.append(root.val)
          else: 
              traverseLeaf(root.left, sequence)
              traverseLeaf(root.right, sequence)
                  
      traverseLeaf(root1, l1)
      traverseLeaf(root2, l2)
          
      return l1 == l2
  </code></pre>
  <p>You can see I reference the list object outside the function scope. However, if I try this:</p>
  <pre><code># class TreeNode:
  #     def __init__(self, val=0, left=None, right=None):
  #         self.val = val
  #         self.left = left
  #         self.right = right
  class Solution:
      def convertBST(self, root: TreeNode) -&gt; TreeNode:
          total = 0
                 
          def rightOrderTraversal(node: TreeNode):
              if not node: return
              elif not node.left and not node.right: 
                  total += node.val
                  node.val = total
              else: 
                  rightOrderTraversal(node.right)
                  total += node.val
                  node.val = total
                  rightOrderTraversal(node.left)                
              
          rightOrderTraversal(root)
          return root
  </code></pre>
  <p>It does not work, and the error says I reference total as a local variable when calling <code>rightOrderTraversal</code>. What kind of variables does Python create local references for? From these examples, it seems like Python references lists outside the function scope but not ints.</p>
  <p>I also tried to make total global as a way to solve the issue, but that didn't work.</p>