can i use rusts match in python? [closed]

2024/11/24 0:53:52

i want to use rust match in python3,

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 value1 => println!("One!"),// Match several values2 | 3 | 5 | 7 | 11 => println!("This is a prime"),// Match an inclusive range13...19 => println!("A teen"),// Handle the rest of cases_ => println!("Ain't special"),}let boolean = true;// Match is an expression toolet binary = match boolean {// The arms of a match must cover all the possible valuesfalse => 0,true => 1,// TODO ^ Try commenting out one of these arms};println!("{} -> {}", boolean, binary);
}
Answer

Thanks for the hint, Andrea Corbellini.

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 work2, "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")))

--

EDIT: Now (2020/06), there is official draft for match PEP 622

https://en.xdnf.cn/q/120590.html

Related Q&A

Consolidating Elements of a List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

html textarea via python using post function

<div><textarea cols="25" rows="12" name="box" id="box" tabindex="2">TEXT GOES HERE </textarea></div><div><p class=&quo…

Fibonacci in Python - Simple solution [duplicate]

This question already has answers here:How to write the Fibonacci Sequence?(67 answers)Closed 6 years ago.n1 = 1 n2 = 1 n3 = n1 + n2 for i in range(10):n1 + n2print(n3)n1 = n2n2 = n3According to what …

snake game: snake colliding with itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I th…

python3 function not defined even though it is

when I try to call the changeProfile function, I keep getting the error "getAuthCode is not defined" even though it is clearly defined. Why do I keep getting this error? What am I doing wron…

Python maximum and minimum

Im supposed to write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a t…

Get differences between two excel files

Problem SummaryGiven 2 excel files, each with 200 columns approx, and have a common index column - ie each row in both files would have a name property say, what would be the best to generate an output…

Most pythonic way to call a list of functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Unable to get wanted output using for and range functions

For my homework assignment, I am using the for loop and the range function. I have to create a loop that printsHello 0 Hello 1 Hello 3 Hello 6 Hello 10The question says that the number corresponds to t…

i want to add a new field to an existing module odoo11 but i dont know why it didnt work

product_template.xmlthe view to add the customizing field to the product module<?xml version="1.0" encoding="utf-8"?><odoo><data><record id="product_temp…