Understanding variable scope in nested functions in Python

ε祈祈猫儿з 提交于 2021-01-28 03:14:59

问题


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.


回答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.



来源:https://stackoverflow.com/questions/56303426/understanding-variable-scope-in-nested-functions-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!