On 16/08/2020 20:12, Klaus Jantzen wrote:
Hi,
the other day I came across the book "Classic Computer Science Problems
in Python" by David Kopec.
The function definitions in the examples like
=====
def fib2(n: int) -> int:
if n < 2: # base case
return n
return fib2(n - 2) + fib2(n - 1) # recursive case
if __name__ == "__main__":
print(fib2(5))
print(fib2(10))
=====
use a syntax that I have never seen on this list or in other publications.
About which line of code are you asking?
> def fib2(n: int) -> int:
Please review: Type Hints:
https://docs.python.org/3/library/typing.html
> if __name__ == "__main__":
https://docs.python.org/3/library/__main__.html
https://docs.python.org/3/reference/import.html
My questions:
Is that new?
Typing: v3.5+ (IIRC)
Is is 'recommended' to use this is the future?
Yes and no. The operative word is "Hints". Typing is not required by
Python. However, you may find the extra error-checking helpful...
I can only see a certain advantage of using this type of function
definition in resp. to the documentation, as it does not provide an
automatic check of the type of the argument(s) or of the result as in Java.
There are 'pros' and 'cons'!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list