Why doesn't exec capture underscore prefixed symbols?

why doesn’t exec capture underscore named variables in it’s execution context?

for the program below i can only see “mergesort” in global/local context but not any of the “_xxx” names.

def mergesort(A):
  def _merge(A, left, m, right):
    left_A, right_A = A[left:m], A[m:right]
    left_len, right_len = len(left_A), len(right_A)
    
    l_i, r_i, k = 0, 0, left
    while l_i < left_len and r_i < right_len:
      if left_A[l_i] <= right_A[r_i]:
        A[k] = left_A[l_i]
        l_i += 1
        k += 1
      else:
        A[k] = right_A[r_i]
        r_i += 1
        k += 1

    while l_i < left_len:
      A[k] = left_A[l_i]
      l_i += 1
      k += 1
    while r_i < right_len:
      A[k] = right_A[r_i]
      r_i += 1
      k += 1
    
      
      
  def _mergesort(A, left, right):
    if left < right:
      m = (left + right)//2
      _mergesort(A, m+1, right)
      _mergesort(A, left, m)
      _merge(A, left, m+1, right+1)
        
    return(A)
  return(_mergesort(A, 0, len(A)))

Those functions are nested within the outer function, meaning that they’re local variables, and only exist while the outer function is running. It doesn’t have anything to do with underscores.

1 Like

should the symbol names exist in the local context then?

i’m trying to collect a function’s binary operations. if a function calls other nested functions, i perform a depth search using the function’s code object’s co_names attribute and keep visiting until I reach the end. for a function like the one mentioned above, i don’t see anything in the execution context or the code object’s co_names.

ah, i see it inside codeobj.co_consts