用法:
class ast.match_case(pattern, guard, body)
match
語句中的單個案例模式。pattern
包含將匹配主題的匹配模式。請注意,為模式生成的AST
節點與為表達式生成的節點不同,即使它們共享相同的語法。guard
屬性包含一個表達式,如果模式與主題匹配,則將評估該表達式。body
包含在模式匹配並且評估保護表達式的結果為真時要執行的節點列表。>>> print(ast.dump(ast.parse(""" ... match x: ... case [x] if x>0: ... ... ... case tuple(): ... ... ... """), indent=4)) Module( body=[ Match( subject=Name(id='x', ctx=Load()), cases=[ match_case( pattern=MatchSequence( patterns=[ MatchAs(name='x')]), guard=Compare( left=Name(id='x', ctx=Load()), ops=[ Gt()], comparators=[ Constant(value=0)]), body=[ Expr( value=Constant(value=Ellipsis))]), match_case( pattern=MatchClass( cls=Name(id='tuple', ctx=Load()), patterns=[], kwd_attrs=[], kwd_patterns=[]), body=[ Expr( value=Constant(value=Ellipsis))])])], type_ignores=[])
相關用法
- Python ast.MatchClass用法及代碼示例
- Python ast.ListComp用法及代碼示例
- Python ast.Lambda用法及代碼示例
- Python ast.IfExp用法及代碼示例
- Python ast.Return用法及代碼示例
- Python ast.Subscript用法及代碼示例
- Python ast.alias用法及代碼示例
- Python ast.Slice用法及代碼示例
- Python ast.NamedExpr用法及代碼示例
- Python ast.MatchAs用法及代碼示例
- Python ast.Try用法及代碼示例
- Python ast.MatchValue用法及代碼示例
- Python ast.Assert用法及代碼示例
- Python ast.Break用法及代碼示例
- Python ast.Load用法及代碼示例
- Python ast.Set用法及代碼示例
- Python ast.MatchStar用法及代碼示例
- Python ast.Expr用法及代碼示例
- Python ast.Attribute用法及代碼示例
- Python ast.ImportFrom用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 ast.match_case。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。