Python:从单独的函数调用一个函数中的变量但不使用全局变量

发布于 2024-12-09 12:24:04 字数 1864 浏览 0 评论 0原文

我编写了下面的代码来检查三个文件,无论哪个文件存在,都对文件运行“扫描”(如果文件不存在,不用担心,只需对可用文件运行“扫描”)并在这些可用文件上生成正确的输出文件。

我正在开发的程序包含以下代码:

def InputScanAnswer():
    scan_number = raw_input("Enter Scan Type number: ")
    return scan_number

此函数检查这三个文件是否存在,如果存在,则为 hashcolumnfilepathNum 分配特定值

def chkifexists():
    list = ['file1.csv', 'file2.csv', 'file3.csv']
    for filename in list:
        if os.path.isfile(filename):
            if filename == "file1.csv":
                hashcolumn = 7
                filepathNum = 5
            if filename == "file2.csv":
                hashcolumn = 15
                filepathNum = 5
            if filename == "file3.csv":
                hashcolumn = 1
                filepathNum = 0
            #print filename, hashcolumn, filepathNum


def ScanChoice(scan_number):
    if scan_number == "1":
        chkifexists()
        onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) #this is what is giving me errors...
    elif scan_number == "2":
        print "this is scan #2"
    elif scan_number =="3":
        print "this is scan #3"
    else:
        print "Oops! Invalid selection. Please try again."


def onlinescan(FileToScan, ResultsFile, hashcolumn, filepathNum):
    # web scraping stuff is done in this function

。遇到的问题是未定义全局名称“filename”。 我意识到问题是我试图将局部变量从 chkifexists() 发送到 onlinescan() 参数。我尝试

return filename
return hashcolumn
return filepathNum

在 chkifexists() 函数末尾使用,但这也不起作用。 下做我想做的事情

onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) 

无论如何,是否可以在不使用全局变量的情况 ?我知道他们很沮丧,我希望我能以另一种方式解决这个问题。另外,onlinescan() 中的 hashcolumnfilepathNum 参数与此有什么关系吗?

I wrote the below code in order to check for three files and whichever files exist, run a "scan" on the file (if a file does not exist, don't worry about it just run a "scan" on the available files) and produce the proper output file on those available files.

The program I'm working on includes the following code:

def InputScanAnswer():
    scan_number = raw_input("Enter Scan Type number: ")
    return scan_number

This function checks if these three files exist and if so, assign specific values to hashcolumn and to filepathNum

def chkifexists():
    list = ['file1.csv', 'file2.csv', 'file3.csv']
    for filename in list:
        if os.path.isfile(filename):
            if filename == "file1.csv":
                hashcolumn = 7
                filepathNum = 5
            if filename == "file2.csv":
                hashcolumn = 15
                filepathNum = 5
            if filename == "file3.csv":
                hashcolumn = 1
                filepathNum = 0
            #print filename, hashcolumn, filepathNum


def ScanChoice(scan_number):
    if scan_number == "1":
        chkifexists()
        onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) #this is what is giving me errors...
    elif scan_number == "2":
        print "this is scan #2"
    elif scan_number =="3":
        print "this is scan #3"
    else:
        print "Oops! Invalid selection. Please try again."


def onlinescan(FileToScan, ResultsFile, hashcolumn, filepathNum):
    # web scraping stuff is done in this function

The error that I run into is global name 'filename' is not defined.
I realize that the problem is I'm attempting to send local variables from chkifexists() to the onlinescan() parameters. I tried using

return filename
return hashcolumn
return filepathNum

at the end of the chkifexists() function but that was not working either. Is there anyway to do what I'm trying to do in the

onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) 

line without using global variables? I know they are discouraged and I'm hoping I can go about it another way. Also, does having hashcolumn and filepathNum parameters in onlinescan() have anything to do with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

古镇旧梦 2024-12-16 12:24:04

在 chkifexists 内,您将返回所有三个变量,如下所示:

return (filename, hashcolumn, filepathNum)

您将通过调用函数来检索这些变量,如下所示:

(filename, hashcolumn, filepathNum) = chkifexists()

现在,您可以将它们包含在函数作用域中,而无需全局变量!

从技术上讲,您也不需要括号。事实上,我不确定为什么我把它们包括在内。但无论哪种方式都有效,那又怎么样。

Inside chkifexists, you would return all three variables like so:

return (filename, hashcolumn, filepathNum)

You would retrieve these by calling the function like so:

(filename, hashcolumn, filepathNum) = chkifexists()

You now have them in your function scope without needing global variables!

Technically, you don't need the parenthesis, either. In fact, I'm not sure why I included them. But it works either way, so what the heck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文