Guido van Rossum
Ts'ebetsong e ncha e tšoana le "match" opareitara e fanoeng ho Scala, Rust, le F #, e bapisang sephetho sa polelo e boletsoeng le lethathamo la lipaterone tse thathamisitsoeng ka har'a li-blocks tse thehiloeng ho "case" operator. Ho fapana le opareitara ea "switch" e fumanehang ho C, Java, le JavaScript, mantsoe a "match" a fana ka tse ling tse ngata.
def http_error(boemo):
boemo ba papali:
nyeoe ea 400:
khutlisetsa "Kopo e mpe"
nyeoe 401|403|404:
khutlisa "Ha ea lumelloa"
nyeoe ea 418:
khutla "Ke 'na teapot"
case_:
return "Ho hong hape"
Mohlala, o ka manolla lintho, li-tuples, manane, le tatellano e sa reroang ho kopanya mefuta e ipapisitseng le boleng bo teng. E lumelloa ho hlalosa litempele tse behiloeng, sebelisa maemo a eketsehileng a "haeba" ka har'a template, sebelisa limaske ("[x, y, * rest]"), limmapa tsa bohlokoa / tsa boleng (mohlala, {"bandwidth": b, "latency ”: l} ho ntša "bandwidth" le "latency" le dikishinari), ntša li-subtemplates (":=" opareitara), sebelisa li-constants ho template. Ka litlelaseng, hoa khonahala ho hlophisa boitšoaro bo nyallanang ka mokhoa oa "__match__()".
ho tsoa ho li-dataclass import dataclass
@dataclass
ntlha ea sehlopha:
x:ine
y:ine
def whereis(point):
ntlha ea papali:
Ntlha ea nyeoe(0, 0):
hatisa ("Origin")
Ntlha ea nyeoe(0, y):
hatisa(f"Y={y}")
Ntlha ea nyeoe(x, 0):
hatisa(f"X={x}")
Ntlha ea nyeoe():
print("Sebakeng se seng")
case_:
print("Ha se ntlha")
ntlha ea papali:
case Ntlha(x, y) haeba x == y:
hatisa(f"Y=X ho {x}")
Ntlha ea nyeoe(x, y):
hatisa (f"Eseng ho diagonal")
KHIBEDU, THARA, PUTSOA = 0, 1, 2
'mala o tšoanang:
case .RED:
print("Ke bona khubelu!")
case .REEN:
print("Bojang bo botala")
nyeoe .BLU
E:
print(“Ke ikutloa ke le mothating :(“)
Sehlopha se lokiselitsoe ho hlahlojoa
Source: opennet.ru