在方法之间共享值
Sharing values between methods
我以为我知道如何在 Python 中使用 return,但出现了问题,我不明白。
class Projet(object):
def pathDirectory(self):
pathDir= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
def goFunc(self, pathDir):
# do function
# HERE pathDir is a boolean and not a str with the path directory
if __name__ == "__main__":
p = Projet()
pathDir = p.pathDirectory()
p.goFunc(pathDir) ## This is the line where it begins
所以我有一个函数可以获取变量中的路径目录并 return 它。
我想在其他函数中使用路径目录,但是当我调用它时,它不再是一个字符串,而是一个 boolean(我得到一个 False 当我打印 pathDir
)
更新:抱歉,伙计们,输入错误,它是 pathDir 而不是路径,但仍然 returns False
你打错了。
def pathDirectory(self):
path= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
应该是:
def pathDirectory(self):
pathDir = str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
因为你正在设置 path
然后不返回它。
def pathDirectory(self):
path= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return path #edited
您应该将 return
语句更改为 return path
,因为 path
将值存储在上一行中。
这应该有效。您可以创建 class 的成员变量,而不是不必要地传递变量。这个变量可以被任何其他函数更新和重用,而不必担心传递参数。
class Projet(object):
def pathDirectory(self):
print "- - in pathDirectory - -"
self.pathDir= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
def goFunc(self):
print "- - In goFunc - -"
print self.pathDir
if __name__ == "__main__":
p = Projet()
p.pathDirectory()
p.goFunc()
我以为我知道如何在 Python 中使用 return,但出现了问题,我不明白。
class Projet(object):
def pathDirectory(self):
pathDir= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
def goFunc(self, pathDir):
# do function
# HERE pathDir is a boolean and not a str with the path directory
if __name__ == "__main__":
p = Projet()
pathDir = p.pathDirectory()
p.goFunc(pathDir) ## This is the line where it begins
所以我有一个函数可以获取变量中的路径目录并 return 它。
我想在其他函数中使用路径目录,但是当我调用它时,它不再是一个字符串,而是一个 boolean(我得到一个 False 当我打印 pathDir
)
更新:抱歉,伙计们,输入错误,它是 pathDir 而不是路径,但仍然 returns False
你打错了。
def pathDirectory(self):
path= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
应该是:
def pathDirectory(self):
pathDir = str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return pathDir
因为你正在设置 path
然后不返回它。
def pathDirectory(self):
path= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
return path #edited
您应该将 return
语句更改为 return path
,因为 path
将值存储在上一行中。
这应该有效。您可以创建 class 的成员变量,而不是不必要地传递变量。这个变量可以被任何其他函数更新和重用,而不必担心传递参数。
class Projet(object):
def pathDirectory(self):
print "- - in pathDirectory - -"
self.pathDir= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))
def goFunc(self):
print "- - In goFunc - -"
print self.pathDir
if __name__ == "__main__":
p = Projet()
p.pathDirectory()
p.goFunc()