使用switcher / python的Switch语句

问题描述 投票:0回答:1

我是第一次使用python制作switch语句。我在以下代码中不断收到错误auth isn't identified。 switch语句的参数应该是msgparam中的第一个元素,它是字符串列表

class SendMessage :

    def Auth_process(message):
        val2 =0
        for flag in message:
            if(flag == '-raw'):
                val1 |= 0x01
            elif(flag == '-v'):
                val1 |= 0x02  
            elif(flag == '-p'):
                val1 |= 0x04
            elif( flag == '-smple'):
                val1 = 0x08
            else:
                val1 = val1
        return Auth_process( val1, val2)


    def command_process(arg, *args):
       switcher ={
            auth : Auth_process,  ////error occurs here
              rd : read,
        }
       func = switcher.get(arg)
       return func(args)

    def __init__(self, cmd):
        status = False
        value1= 0
        value2= 0
        result = '"This commandself is invalid please check arguments for appropriate size or invalid characters";'
        msgparam = cmd.split(' ')
        print(msgparam)
        self.command_process(msgparam[0], msgparam)

if __name__ == '__main__':
    data =  SendMessage("auth -v -raw")
python-3.x switch-statement
1个回答
0
投票

您的字典键设置为变量(错误提示,当您尝试引用它们时未定义),而不是字符串。您想要:

switcher ={
    'auth' : Auth_process,
    'rd' : read,
}

您还需要确保read是该类的已定义方法。

© www.soinside.com 2019 - 2024. All rights reserved.