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

标签 python python-3.x

我正在尝试在 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
}

关于python - 问题,在 python 3.7 中构建开关盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928244/

相关文章:

python - 如何在Python中从串行数据包中提取字节序列并将其更改为正确的整数表示形式?

python - 在python中通过继承创建自定义float类

python - 转置和连接字符串

python - 如何从列表中收集范围?

python - list.count() 表示列表中有一项时有两项

python - SQL 更新语句但使用 pyodbc

python - 渲染文本日历

python - 从 csv 文件中加载 url 列表,并针对相同的数据对它们进行逐一解析

python - 如何使用列表理解从列表中的集合中删除特定元素

python - 流式传输 Twitter 推文时忽略转发