首页 > 编程语言 >python switch 替换if else

python switch 替换if else

时间:2022-08-21 12:47:09浏览次数:59  
标签:__ python else switch 404 401 400 print lambda

1,python 解释器版本3.10以上可以使用如下

def dar(darling):
    match darling:
        case '400':
            print(400)
        case '401':
            print(401)
        case _:
            print(404)


if __name__ == '__main__':
    dar('400')

解释器版本,小于3.10 会报错SyntaxError: invalid syntax

2,所有版本


status = {
    '400': lambda: stat400('400'),
    '401': lambda: stat401(),
    '404': lambda: print('我是404')

}

def stat400(x):
    print('我是400' + x)

def stat401():
    print('我是401')

if __name__ == '__main__':
    status.get('404', lambda: print('未找到'))()

标签:__,python,else,switch,404,401,400,print,lambda
From: https://www.cnblogs.com/darling331/p/16609789.html

相关文章

  • Python小游戏——外星人入侵(保姆级教程)第一章 05
    系列文章目录第一章:武装飞船05:重构:模块game_functions一、重构在大型项目中,经常需要在添加新代码前重构既有代码。重构旨在简化既有代码的结构,使其更容易扩展。在本节......
  • python 时间戳装饰器
    点击查看代码importtimefromfunctoolsimportwrapsdeftimer(func):@wraps(func)definner(*args,**kwargs):start=time.time()re......
  • 牛客网笔试输入输出处理方法总结(基于Python3.5)
    牛客网判题系统输入处理牛客网上的输入输出借鉴ACM模式给出,对于习惯了leetcode函数定义形式解题的小伙伴们来说确实比较生疏。为了避免在之后的笔试中再次吃亏,在这里对牛......
  • python文件上传
    前端代码:html<inputid="fileUpload"type="file"name="upload"><inputtype="button"@click="submitfile"value="Upload">jssubmitfile(){......
  • python wraps装饰器
    fromfunctoolsimportwrapsdefdecorator(func):"""thisisdecorator__doc__"""@wraps(func)defwrapper(*args,**kwargs):"""thisisw......
  • python输入和类型转换
    输入获取用户使用键盘录入的内容使用的函数是input()变量=input(‘提示的信息’)1.代码从上到下执行,遇到input函数之后 类型转换根据代码的需要,将一种数据类型......
  • Notepad plus 通过NppExec插件编译/运行 golang,php,python等语言
        1. 在Notepadplus的插件-->插件管理中,添加nppExec插件。          2.打开插件-->NppExec,选择Showconsole,和Follow($CURRE......
  • python---re
    python---repython的re模块简单使用re.findall,re.compile,re.match和re.searchre.findall这个是最好用的,查找所有符合条件的,返回list,或Noneimportrestr1='gdf......
  • python---struct
    python---structkeywords:structbytespython数据互转https://docs.python.org/3/library/struct.html二进制数据和各种类型数据的转换因为不同平台默认大小端不同,......
  • Python custom modify the __add__ method All In One
    PythoncustommodifytheaddmethodAllInOnePython改写__add__类方法"""#classJuice:#def__init__(self,name,capacity):#self.na......