【问题标题】:Python Bulk 'If' StatementsPython 批量“如果”语句
【发布时间】:2019-12-13 22:34:26
【问题描述】:

我在什么应该是一个简单的问题上画了一个空白,所以感谢任何帮助。

有没有更好的方法来处理所有这些“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

【问题讨论】:

  • 是的,如果你有一堆重复的代码,你可以把它放在一个函数中,每次需要调用代码时调用那个函数。
  • “既然它们都是相同的 'if' 语句,我可以为它们创建一个专用函数,然后将它们调用到每个函数中吗?” 是的,这是有道理的。注意,你应该使用if elif else 来提高效率
  • 谢谢你们。有道理,我也会用 if elif else。

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


【解决方案1】:

您可以使用字典来分配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 语句。否则,Chris 的答案更适合。

更多可能的相关答案here

【讨论】:

  • 注意:dict 越大,在函数外部定义它就越重要;正如所写,您在每次调用时都从头开始重建它,因此您不会以任何有意义的方式减少工作量。
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回复。这很有意义。最后一个问题,我希望对每个功能执行不同的操作。我怎么能打那个电话?举个例子,我可以把 print('add') 放在 btn_add_printer 函数中,把 print('remove') 放在 btn_remove_printer 函数中吗?
  • 是的,你可以在每个函数中做任何你想做的事情。例如,您的打印机函数可以返回 netowkr 打印机。当您在 add 或 remove 中调用该函数时,您可以存储 netowkr 打印机,然后在 add 或 remove 函数中使用它做任何您喜欢的事情。他们可以用网络打印机做不同的事情。关键是我们现在在它自己的函数中有通用代码
【解决方案3】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多