了解Python嵌套函数中的变量作用域 [英] Understanding variable scope in nested functions in Python

查看:65
本文介绍了了解Python嵌套函数中的变量作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python3.7中具有以下功能

  def output_report():sheet_dict = {1:全部",2:风",3:"Soalr"}sheet_name = sheet_dict [sheet_num]如果不是row_num,则file_name = f'{file_name} _ {sheet_name} _row_ {row_num} .csv'f'{file_name} _ {sheet_name} .csv'返回文件名def test_file(x):file_name = output_report(sheet_num)返回f'{x} _ {file_name}'def good_func():sheet_num = 2row_num = 2一个= test_file('新文件')返回一个 

当我打电话时: good_func()

它会引发以下错误:

NameError:名称'sheet_num'未定义

但是,如果我在全局范围内定义sheet_name和row_num,

  sheet_num = 2row_num = 2def good_func():一个= test_file('新文件')返回一个 

代码有效.

我的问题:我的理解是,在嵌套函数中,内部函数开始从自身查找变量,然后进入外部函数,最后进入全局范围.然后,我希望第一个函数也可以运行,但事实并非如此.那是什么?我阅读了其他范围相关的问题,但没有找到我的答案.

解决方案

第一种情况

  def good_func():sheet_num = 2row_num = 2一个= test_file('新文件')返回一个 

sheet_num row_num 在函数 good_func 本地,因此无法在另一个函数 output_report <中访问/p>

但是当您这样做

  sheet_num = 2row_num = 2def good_func():一个= test_file('新文件')返回一个 

sheet_num row_num 成为所有其他函数均可访问的全局变量,因此它们也可在 output_report 中访问

也嵌套函数是其定义位于另一个函数之内的函数,其中 a 可在 inner

中访问

  def external():a = 1def inner():打印(a)内()外() 

像在 good_func 中一样在函数内部调用另一个函数不会使它们 output_function 嵌套.

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.

解决方案

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.

这篇关于了解Python嵌套函数中的变量作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆