更新时间:2023-08-22 14:55:34
除非已定义函数,否则无法调用该函数.将def createDirs():
块移至文件顶部,位于导入的下方.
You can't call a function unless you've already defined it. Move the def createDirs():
block up to the top of your file, below the imports.
某些语言允许您在定义函数之前使用它们.例如,javascript将此称为吊装".但是Python并不是其中的一种语言.
Some languages allow you to use functions before defining them. For example, javascript calls this "hoisting". But Python is not one of those languages.
请注意,只要定义是在使用之前创建的,就可以在比创建函数的行高的行中引用该函数.例如,这是可以接受的:
Note that it's allowable to refer to a function in a line higher than the line that creates the function, as long as chronologically the definition occurs before the usage. For example this would be acceptable:
import os
def doStuff():
if os.path.exists(r'C:\Genisis_AI'):
print("Main File path exists! Continuing with startup")
else:
createDirs()
def createDirs():
os.makedirs(r'C:\Genisis_AI\memories')
doStuff()
即使在第7行调用了createDirs()
并且在第9行对其进行了定义,这也不是问题,因为def createDirs
在doStuff()
在第12行执行之前就执行了.
Even though createDirs()
is called on line 7 and it's defined on line 9, this isn't a problem because def createDirs
executes before doStuff()
does on line 12.