Python:从单独的函数调用一个函数中的变量但不使用全局变量
我编写了下面的代码来检查三个文件,无论哪个文件存在,都对文件运行“扫描”(如果文件不存在,不用担心,只需对可用文件运行“扫描”)并在这些可用文件上生成正确的输出文件。
我正在开发的程序包含以下代码:
def InputScanAnswer():
scan_number = raw_input("Enter Scan Type number: ")
return scan_number
此函数检查这三个文件是否存在,如果存在,则为 hashcolumn
和 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
。遇到的问题是未定义全局名称“filename”
。 我意识到问题是我试图将局部变量从 chkifexists() 发送到 onlinescan() 参数。我尝试
return filename
return hashcolumn
return filepathNum
在 chkifexists() 函数末尾使用,但这也不起作用。 下做我想做的事情
onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum)
无论如何,是否可以在不使用全局变量的情况 ?我知道他们很沮丧,我希望我能以另一种方式解决这个问题。另外,onlinescan()
中的 hashcolumn
和 filepathNum
参数与此有什么关系吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 chkifexists 内,您将返回所有三个变量,如下所示:
您将通过调用函数来检索这些变量,如下所示:
现在,您可以将它们包含在函数作用域中,而无需全局变量!
从技术上讲,您也不需要括号。事实上,我不确定为什么我把它们包括在内。但无论哪种方式都有效,那又怎么样。
Inside
chkifexists
, you would return all three variables like so:You would retrieve these by calling the function like so:
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.