Consider ML-style? :D ``` def fib(n): | n == 0 = 0 | n == 1 = 1 | otherwise = fib(n-1) + fib(n-2) def allow_entry(d): | d == {"name": "Bob"} = "Bob is not allowed in ever!" | d == {"name": "Charles", "day": "Tuesday"} = \ "It's a Tuesday, so Charles is allowed in." | d == {"name": "Charles", "day": _}) = \ "Charles is only allowed in on a Tuesday." | d == {"name": name} = f"Come in {name}, make yourself at home!" ``` On 05/08/2021 16:39, Sam Frances wrote:
Following on from PEP 634, it would be good to be able to have Erlang / Elixir-style pattern matching in function headers.
(I don't know what the technical term is for this language feature, but hopefully the examples will clarify.)
Here's the obligatory fibonacci example:
``` def fib(0): return 0
def fib(1): return 1
def fib(n): return fib(n-1) + fib(n-2) ```
Or, another example:
``` def allow_entry({"name": "Bob"}): return "Bob is not allowed in ever!"
def allow_entry({"name": "Charles", "day": "Tuesday"}): return "It's a Tuesday, so Charles is allowed in."
def allow_entry({"name": "Charles", "day": _}): return "Charles is only allowed in on a Tuesday."
def allow_entry({"name": name}): return f"Come in {name}, make yourself at home!" ```
Thanks for considering my idea.
Kind regards
Sam Frances _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OPLBLW... Code of Conduct: http://python.org/psf/codeofconduct/