Python 3.x 通过另一个方法退出lambda表达式

Python 3.x 通过另一个方法退出lambda表达式,python-3.x,lambda,Python 3.x,Lambda,我有一个程序,通过点击按钮开始数据类型转换。我正在为我的按钮命令使用lambda函数。转换开始前,方法会检查是否已选择输入。现在,如果没有选择输入文件,我会在jupyter笔记本中收到一条错误消息,这不一定是个问题,因为数据类型转换无论如何都不应该开始。然而,我问自己是否有办法阻止lambda函数的继续。我将添加一段代码,并进一步解释我的意思: 我的按钮命令: self.program_start["command"]=lambda:[self.fileselectwarning(),self.

我有一个程序,通过点击按钮开始数据类型转换。我正在为我的按钮命令使用lambda函数。转换开始前,方法会检查是否已选择输入。现在,如果没有选择输入文件,我会在jupyter笔记本中收到一条错误消息,这不一定是个问题,因为数据类型转换无论如何都不应该开始。然而,我问自己是否有办法阻止lambda函数的继续。我将添加一段代码,并进一步解释我的意思:

我的按钮命令:

self.program_start["command"]=lambda:[self.fileselectwarning(),self.writealldatafile(),self.writeselecteddata(),
                                         self.inputliste.clear(),self.fileopeningcounter.set(0),
                                         self.inputfilenamelist.clear()]
检查是否已选择输入/输出文件的方法:

def fileselectwarning(self):
    stringliste=[self.fileopenname.get(),self.filesavename.get()]
    if stringliste[0]=="" and not stringliste[1]=="":
        self.messagebox1 = messagebox.showwarning("Missing Inputfile","No Inputfile selected, please select one and retry!")
    elif not stringliste[0]=="" and stringliste[1]=="":
        self.messagebox2 = messagebox.showwarning("Missing Outputfile","No Outputfilename selected, please select one and retry!")
    elif stringliste[0]=="" and stringliste[1]=="":
        self.messagebox3 = messagebox.showwarning("Missing Files","Neither Input nor Outputfile were selected, please select both and retry!")
    elif not stringliste[0]=="" and not stringliste[1]=="":
        ausfuehrenderdatenverarbeitung=self.zugriffaufdatenverarb()
因此,在我的方法fileselectwarning中,当同时选择了输入和输出文件时,将调用另一个方法,该方法将启动部分转换。然后调用按钮lambda函数中的所有其他方法,这些方法依赖于通过“fileselectwarning”方法调用的方法中创建的列表。但是,如果输入或输出文件丢失,lambda功能将继续,并且这些列表尚未创建,因此会产生错误

总结一下,有没有办法阻止lambda函数继续
并在“fileselectwarning”方法中实现它?

编写一个常规函数,而不是滥用lambda表达式中的列表构造函数来调用函数作为副作用

为了中止执行,可以使用自定义异常,该异常由
fileselectwarning
引发。该功能本身也可以简化

class MissingFiles(Exception):
    pass

class ...:
    def fileselectwarning(self):
        infile, outfile = self.fileopenname.get(), self.filesavename.get()
        if not infile and outfile:
            raise MissingFiles("No Inputfile selected, please select one and retry!")
        elif infile and not outfile:
            raise MissingFiles("No Outputfilename selected, please select one and retry!")
        elif not infile and not outfile:
            raise MissingFiles("Neither Input nor Outputfile were selected, please select both and retry!")
        else:
            ausfuehrenderdatenverarbeitung=self.zugriffaufdatenverarb()

    def start_command(self):
        try:
            self.fileselectwarning()
        except MissingFiles as e:
            self.messagebox1 = messagebox.showwarning("Missing files", str(e))
            return
        self.writealldatafile()
        self.writeselecteddata()
        self.inputliste.clear()
        self.fileopeningcounter.set(0)
        self.inputfilenamelist.clear()
然后,在分配命令时,执行以下操作:

    self.program_start["command"] = self.start_command

显然,我无法测试它,因此它可能无法立即工作,但它显示了总体思路。

与其在lambda表达式中滥用列表构造函数来调用函数作为副作用,不如编写一个常规函数

为了中止执行,可以使用自定义异常,该异常由
fileselectwarning
引发。该功能本身也可以简化

class MissingFiles(Exception):
    pass

class ...:
    def fileselectwarning(self):
        infile, outfile = self.fileopenname.get(), self.filesavename.get()
        if not infile and outfile:
            raise MissingFiles("No Inputfile selected, please select one and retry!")
        elif infile and not outfile:
            raise MissingFiles("No Outputfilename selected, please select one and retry!")
        elif not infile and not outfile:
            raise MissingFiles("Neither Input nor Outputfile were selected, please select both and retry!")
        else:
            ausfuehrenderdatenverarbeitung=self.zugriffaufdatenverarb()

    def start_command(self):
        try:
            self.fileselectwarning()
        except MissingFiles as e:
            self.messagebox1 = messagebox.showwarning("Missing files", str(e))
            return
        self.writealldatafile()
        self.writeselecteddata()
        self.inputliste.clear()
        self.fileopeningcounter.set(0)
        self.inputfilenamelist.clear()
然后,在分配命令时,执行以下操作:

    self.program_start["command"] = self.start_command

显然,我无法测试它,因此它可能无法立即工作,但它显示了总体思路。

与其滥用列表构造函数调用函数作为副作用,不如编写一个常规函数。您能详细说明一下,我应该在哪里编写常规函数吗?而不是lambda表达式?是的。我试着写一个答案来说明我的意思。与其滥用列表构造函数来调用函数作为副作用,不如编写一个正则函数。你能详细说明一下,我应该在哪里编写正则函数吗?而不是lambda表达式?是的。我试着写一个答案来说明我的意思。你可以使用常规方法、实例方法和传入self作为参数,也可以不使用self的staticmethods。我的意思是,它在语法上是完全有效的,它只是不会表现出你所期望的样子。@user8407600:如果
start\u命令
在内部使用
self
,你肯定不想让它成为
@staticmethod
,因为调用函数时不会传递实例,而是需要一个实例来调用方法。将
self.start\u命令
分配给
self.program\u start[“command”]
使其成为绑定方法,因此可以不带参数调用它,但仍然正确地传递
self
(因为绑定方法在内部隐藏它)。您可以使用常规的、实例的方法并传入self作为参数,或者没有自我的方法。我的意思是,它在语法上是完全有效的,它只是不会表现出你所期望的样子。@user8407600:如果
start\u命令
在内部使用
self
,你肯定不想让它成为
@staticmethod
,因为调用函数时不会传递实例,而是需要一个实例来调用方法。将
self.start\u命令
分配给
self.program\u start[“command”]
使其成为绑定方法,因此可以无参数调用它,但仍然正确地传递
self
(因为绑定方法在内部隐藏它)。