我可以在python中使用rust的火柴吗? [英] can i use rust's match in python?
本文介绍了我可以在python中使用rust的火柴吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python3中使用rust match
i want to use rust match in python3,
代替if ... elif语句.
instead of if...elif statement.
https://doc.rust-lang.org/rust-by-example/flow_control/match.html
fn main() {
let number = 13;
// TODO ^ Try different values for `number`
println!("Tell me about {}", number);
match number {
// Match a single value
1 => println!("One!"),
// Match several values
2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
// Match an inclusive range
13...19 => println!("A teen"),
// Handle the rest of cases
_ => println!("Ain't special"),
}
let boolean = true;
// Match is an expression too
let binary = match boolean {
// The arms of a match must cover all the possible values
false => 0,
true => 1,
// TODO ^ Try commenting out one of these arms
};
println!("{} -> {}", boolean, binary);
}
推荐答案
感谢提示,Andrea Corbellini.
Thanks for the hint, Andrea Corbellini.
我找到了解决方案,有 pampy
i found that solution, there is pampy
from pampy import match, _
def func(x):
return match(x,
1, "One!",
# 2 | 3 | 5 | 7 | 11, "This is a prime", # not work
# 2 or 3 or 5 or 7 or 11, "This is a prime", # not work
2, "This is a prime",
_, "Ain't special"
)
if __name__ == '__main__':
print("1: {}".format(func(1)))
print("2: {}".format(func(2)))
print("3: {}".format(func(3)))
print("5: {}".format(func(5)))
print("7: {}".format(func(7)))
print("nothing: {}".format(func("nothing")))
-
现在(2020/06),有比赛的正式草案 PEP622
Now (2020/06), there is official draft for match PEP 622
这篇关于我可以在python中使用rust的火柴吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文