2

I have the following functions in Python3.7

def output_report():
    sheet_dict = {1: 'All', 2: 'Wind', 3: 'Soalr'} 
    sheet_name = sheet_dict[sheet_num]
    file_name = f'{file_name}_{sheet_name}_row_{row_num}.csv' if row_num else 
    f'{file_name}_{sheet_name}.csv'
    return file_name

def test_file(x):
    file_name = output_report(sheet_num)
    return f'{x}_{file_name}'

def good_func():
    sheet_num = 2
    row_num = 2
    a = test_file('new file')
    return a

when I call: good_func()

It raises an error that:

NameError: name 'sheet_num' is not defined

But if I define sheet_name and row_num in the global scope like,

sheet_num = 2
row_num = 2
def good_func():
    a = test_file('new file')
    return a

the code works.

My question: My understanding was that in nested functions, the inner functions starts looking for the variables from itself and then goes to outer functions and finally to the global scope. Then, I expected the first function also runs, but that's not the case. What is that? I read other scope related questions but didn't find my answer.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
mk_sch
  • 1,060
  • 4
  • 16
  • 31
  • I don't want to use global variables, as I only read the variable and don't change it. Maybe nanlocal would be an option. – mk_sch May 25 '19 at 09:18

1 Answers1

1

In your first case

def good_func():
    sheet_num = 2
    row_num = 2
    a = test_file('new file')
    return a

sheet_num and row_num are local to the function good_func and hence cannot be accessed in another function output_report

But when you do

sheet_num = 2
row_num = 2
def good_func():
    a = test_file('new file')
    return a

sheet_num and row_num become global variables accessible to all other functions, hence they are accessible in output_report as well

Also nested function are functions whose definition lies within another function like so, where a is accessible in inner

def outer():
    a = 1
    def inner():
        print(a)
    inner()

outer()

Calling another function inside a function like you do in good_func doesn't make them output_function nested.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40