Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,将一个函数的变量用于另一个函数_Python_Function_Inheritance - Fatal编程技术网

在python中,将一个函数的变量用于另一个函数

在python中,将一个函数的变量用于另一个函数,python,function,inheritance,Python,Function,Inheritance,我有一个功能: def main(): bla bla bla... output_name = 'Employee' bla bla bla... def tocsv(path): xls_file = pd.ExcelFile(path) names = xls_file.sheet_names for items in names: df = xls_file.parse(items) out_file = output_na

我有一个功能:

def main():
    bla bla bla...
    output_name = 'Employee'
    bla bla bla...
def tocsv(path):
xls_file = pd.ExcelFile(path)

names = xls_file.sheet_names    

for items in names:     
    df = xls_file.parse(items)
    out_file = output_name + '.csv'
    df.to_csv(out_file, sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE )
我还有一个功能:

def main():
    bla bla bla...
    output_name = 'Employee'
    bla bla bla...
def tocsv(path):
xls_file = pd.ExcelFile(path)

names = xls_file.sheet_names    

for items in names:     
    df = xls_file.parse(items)
    out_file = output_name + '.csv'
    df.to_csv(out_file, sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE )

我需要第一个函数main()中定义的变量“output_name”中的文件名。因此,我的输出文件将命名为“Employee.csv”。我怎么做?提前感谢

如果变量是在函数中定义的,则它存储在局部范围中。要在多个函数中使用变量,有两种方法

第一:将
output\u name
设置为全局变量:

def main():
    global output_name
    output_name = 'Employee'
第二:调用
tocsv()
时传递参数:


函数中定义的变量是该函数的局部变量。如果要在其他地方使用函数内部定义的变量,应
将该变量返回到调用代码。