当变量存在时,为什么我会收到 Flake8 F821 错误?
Why am I getting Flake8 F821 error when the variable exists?
我有一个返回变量的函数,还有一个使用它的函数。在我的 main
func 中,虽然出现了 flake8,但变量未定义。
我尝试将其添加为 global var
,并将 tox.ini
文件放置在与我使用 ignore = F821
的脚本相同的文件夹中,但这也没有注册。 A
有什么建议吗?代码块如下供参考。 new_folder
是罪魁祸首
def createDestination(self):
'''
split the src variable for machine type
and create a folder with 'Evo' - machine
'''
s = src.split('\')
new_folder = (dst + '\Evo ' + s[-1])
if not os.path.exists(new_folder):
os.makedirs(new_folder)
return self.new_folder
def copyPrograms(new_folder):
'''
find all TB-Deco programs in second tier directory.
'''
# create file of folders in directory
folder_list = os.listdir(src)
# iterate the folder list
for folder in folder_list:
# create a new directory inside each folder
folder_src = (src + '\' + folder)
# create a list of the files in the folder
file_list = os.listdir(folder_src)
# iterate the list of files
for file in file_list:
# if the file ends in .part .PART .dbp or .DBP - add it to a list
if (file.endswith('.part') or file.endswith('.PART') or
file.endswith('.dbp') or file.endswith('.DBP')):
# create a location variable for that file
file_src = (src + folder + '\' + file)
# copy the file from the server to dst folder
new_file = ('Evo ' + file)
file_dst = (new_folder + '\' + new_file)
if not os.path.exists(file_dst):
shutil.copy2(file_src, file_dst)
def main():
createDestination()
copyPrograms(new_folder)
if __name__ == "__main__":
main()
第一个问题是 createDestination
从未定义属性 self.new_folder
,只定义了局部变量 new_folder
。缩进也关闭了,因为你想 return new_folder
无论你是否必须先创建它。
def createDestination(self):
'''
split the src variable for machine type
and create a folder with 'Evo' - machine
'''
s = src.split('\')
new_folder = (dst + '\Evo ' + s[-1])
if not os.path.exists(new_folder):
os.makedirs(new_folder)
return new_folder # not self.new_folder
其次,您从未将 createDestination
的 return 值分配给任何名称,以便您可以将其作为参数传递给 copyPrograms
。
def main():
new_folder = createDestination()
copyPrograms(new_folder)
名称具有作用域,createDestination
中名为 new_folder
的变量与 main
中的同名变量不同。作为推论,没有必要使用相同的名称; main
的以下定义同样适用:
def main():
d = createDestination()
copyPrograms(d)
而且您甚至 不需要 命名 return 值;您可以直接将其传递为
def main():
copyPrograms(createDestination())
我有一个返回变量的函数,还有一个使用它的函数。在我的 main
func 中,虽然出现了 flake8,但变量未定义。
我尝试将其添加为 global var
,并将 tox.ini
文件放置在与我使用 ignore = F821
的脚本相同的文件夹中,但这也没有注册。 A
有什么建议吗?代码块如下供参考。 new_folder
是罪魁祸首
def createDestination(self):
'''
split the src variable for machine type
and create a folder with 'Evo' - machine
'''
s = src.split('\')
new_folder = (dst + '\Evo ' + s[-1])
if not os.path.exists(new_folder):
os.makedirs(new_folder)
return self.new_folder
def copyPrograms(new_folder):
'''
find all TB-Deco programs in second tier directory.
'''
# create file of folders in directory
folder_list = os.listdir(src)
# iterate the folder list
for folder in folder_list:
# create a new directory inside each folder
folder_src = (src + '\' + folder)
# create a list of the files in the folder
file_list = os.listdir(folder_src)
# iterate the list of files
for file in file_list:
# if the file ends in .part .PART .dbp or .DBP - add it to a list
if (file.endswith('.part') or file.endswith('.PART') or
file.endswith('.dbp') or file.endswith('.DBP')):
# create a location variable for that file
file_src = (src + folder + '\' + file)
# copy the file from the server to dst folder
new_file = ('Evo ' + file)
file_dst = (new_folder + '\' + new_file)
if not os.path.exists(file_dst):
shutil.copy2(file_src, file_dst)
def main():
createDestination()
copyPrograms(new_folder)
if __name__ == "__main__":
main()
第一个问题是 createDestination
从未定义属性 self.new_folder
,只定义了局部变量 new_folder
。缩进也关闭了,因为你想 return new_folder
无论你是否必须先创建它。
def createDestination(self):
'''
split the src variable for machine type
and create a folder with 'Evo' - machine
'''
s = src.split('\')
new_folder = (dst + '\Evo ' + s[-1])
if not os.path.exists(new_folder):
os.makedirs(new_folder)
return new_folder # not self.new_folder
其次,您从未将 createDestination
的 return 值分配给任何名称,以便您可以将其作为参数传递给 copyPrograms
。
def main():
new_folder = createDestination()
copyPrograms(new_folder)
名称具有作用域,createDestination
中名为 new_folder
的变量与 main
中的同名变量不同。作为推论,没有必要使用相同的名称; main
的以下定义同样适用:
def main():
d = createDestination()
copyPrograms(d)
而且您甚至 不需要 命名 return 值;您可以直接将其传递为
def main():
copyPrograms(createDestination())