Python批量'If'语句

时间:2019-12-13 22:34:26

标签: python python-3.x if-statement simplify

我在应该是一个简单问题的地方空白,以便于任何帮助。

有没有比将它们复制到每个函数中更好的方法来处理所有这些“ if”语句?

这不是全部代码,但应该显示我的问题。由于它们都是相同的'if'语句,我可以为它们创建一个专用函数然后将其调用到每个函数中吗?

def btn_add_printer(self):

    printer = str(self.ui.comboBox.currentText())
    if printer == printer1_name_short:
        network_printer = printer1_name_long

    if printer == printer2_name_short:
        network_printer = printer2_name_long

    if printer == printer3_name_short:
        network_printer = printer3_name_long

def btn_remove_printer(self):

    printer = str(self.ui.comboBox.currentText())
    if printer == printer1_name_short:
        network_printer = printer1_name_long

    if printer == printer2_name_short:
        network_printer = printer2_name_long

    if printer == printer3_name_short:
        network_printer = printer3_name_long

3 个答案:

答案 0 :(得分:2)

您可以使用字典来分配printerX_name_long

def get_network_printer(name_short):
    return {
    printer1_name_short: printer1_name_long,
    printer2_name_short: printer2_name_long
    }.get(name_short, "default")

def btn_add_printer(self):
    network_printer = get_network_printer(self.ui.comboBox.currentText())

这是假设您要避免使用多个if-else语句。否则,克里斯的答案会更合适。

更多可能的相关答案here

答案 1 :(得分:0)

因此,由于两个函数都使用完全相同的代码,因此您可以创建一个执行此代码的函数,然后从其他两个函数中调用该函数。像

class my_class:
    def btn_add_printer(self):
        self.printer_func()

    def btn_remove_printer(self):
        self.printer_func()

    def printer_func(self):
        printer = str(self.ui.comboBox.currentText())
        if printer == printer1_name_short:
            network_printer = printer1_name_long
        elif printer == printer2_name_short:
            network_printer = printer2_name_long
        elif printer == printer3_name_short:
            network_printer = printer3_name_long

        #not sure what you want to do with network_printer but can do it here

答案 2 :(得分:0)

或者,您可以在函数外部定义一个字典,更加紧凑。

def btn_add_printer(self):
    printer = str(self.ui.comboBox.currentText())
    if printer in self.printers: network_printer = printers[printer]

def btn_remove_printer(self):
    printer = str(self.ui.comboBox.currentText())
    if printer in self.printers: network_printer = printers[printer]

self.printers = {
        'printer1_name_short':'printer1_name_long',
        'printer2_name_short':'printer2_name_long',
        'printer3_name_short':'printer3_name_long',
}

或通过字典:

def btn_add_printer(self, printers):
    printer = str(self.ui.comboBox.currentText())
    if printer in printers: network_printer = printers[printer]

def btn_remove_printer(self, printers):
    printer = str(self.ui.comboBox.currentText())
    if printer in printers: network_printer = printers[printer]

printers = {
        'printer1_name_short':'printer1_name_long',
        'printer2_name_short':'printer2_name_long',
        'printer3_name_short':'printer3_name_long',
}

self.btn_add_printer(printers)

或使用辅助功能:

def printerLong(self, printerShort):
    printers = {
        'printer1_name_short':'printer1_name_long',
        'printer2_name_short':'printer2_name_long',
        'printer3_name_short':'printer3_name_long',
    }

    if printerShort in printers:
        return printers[printerShort]


def btn_add_printer(self, printers):
    printer = str(self.ui.comboBox.currentText())
    network_printer = self.printerLong(printer)


def btn_remove_printer(self, printers):
    printer = str(self.ui.comboBox.currentText())
    network_printer = self.printerLong(printer)