Skip to main content
 首页 » 编程设计

python之问题,在 python 3.7 中构建开关盒

2025年05月04日11over140

我正在尝试在 python 3.7 中开发类似 switch-case 功能的东西,就像其他语言已知的那样。

为此,我在这里使用了本教程:https://jaxenter.com/implement-switch-case-statement-python-138315.html

并从这段代码开始:

class ClassCheckShipping: 
 
    def __init__(self): 
        pass 
 
    def __checkAktivweltShipping(self, country): 
        return "checkShipping für Aktivwelt" 
 
    def __checkHoerhelferShipping(self, country): 
        return "checkShipping für Hörhelfer" 
 
    def checkShipping(self, merchant, country): 
        self.country = country 
        switcher = { 
            "Aktivwelt": __checkAktivweltShipping, 
            "Hörhelfer": __checkHoerhelferShipping 
        } 
        func = switcher.get(merchant, lambda: "unbekannter Merchant") 
        print(func()) 

不幸的是,我收到以下错误,但我找不到我的错误。

File "M:\Python-Projekte\Wipando-Feeds\CheckShipping.py", line 18, in checkShipping "Aktivwelt": __checkAktivweltShipping, NameError: name '_ClassCheckShipping__checkAktivweltShipping' is not defined

你能给我一个修复这段代码的提示吗?

请您参考如下方法:

您必须将self 添加到switcher 中的方法:

switcher = { 
    "Aktivwelt": self.__checkAktivweltShipping, 
    "Hörhelfer": self.__checkHoerhelferShipping 
}