For me, it looks normal because I am used to seeing pattern matching for function parameters in functional programming languages. However, if we write “case” before “def” similar to “async” before “def” in async function it should be clear we are doing pattern matching. The function will be named case function. case def fib(0): return 0 case def fib(1): return 1 case def fib(int(n)): return fib(n-1) + fib(n-2) If you none of the parameters you pass match your case functions, an exception will be raised. Maybe it would be easier to make those functions faster since we already know the types in advance. Another solution would be something like this …. def fib(n): case 0, : return 0 case 1, : return 1 case int(n), : return fib(n-1) + fib(n-2) As opposed to … def fib(*args): match args: case 0, : return 0 case 1, : return 1 case int(n), : return fib(n-1) + fib(n-2)
On 6 Aug 2021, at 4:46 PM, 2QdxY4RzWzUUiLuE@potatochowder.com wrote:
On 2021-08-06 at 21:57:47 +1000, Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Aug 05, 2021 at 09:39:44AM +0100, Sam Frances wrote:
def fib(0): return 0
def fib(1): return 1
def fib(n): return fib(n-1) + fib(n-2)
I think that there is something rather disturbing about writing a function definition with a constant literal as parameter. It looks wrong and I'm sure it's going to confuse beginners.
You are not a beginner (nor am I); what's disturbing to you may or may not look wrong or confuse someone else. When I was a beginner, x = x + 1 was disturbing, wrong, and confusing to me.
The proposed definition of the Fibonacci sequence mirrors the ones in Wikipedia¹ and OEIS,² and will certainly be at least familiar to those with a background in mathematics or coming to Python from functional languages.
That said, I agree that it's not a good fit for Python, for reasons expressed elsewhere in this thread.
FWIW, Lisp Flavored Erlang³ (a syntactically imperative language built atop the Erlang VM and run-time) doesn't repeat the "def," only the argument lists:
lfe> (defun ackermann ((0 n) (+ n 1)) ((m 0) (ackermann (- m 1) 1)) ((m n) (ackermann (- m 1) (ackermann m (- n 1)))))
Something like that might work in Python, but at that point, it's no different from a function that matches on its *args argument.
¹ https://en.wikipedia.org/wiki/Fibonacci_number ² https://oeis.org/A000045 ³ https://lfe.io/ _______________________________________________ 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/TCBZOF... Code of Conduct: http://python.org/psf/codeofconduct/