I believe types can be specified in Python as is mentioned here: https://docs.python.org/3/library/typing.html
I am using following simple function modified from one on above page:
def greeting(name: str) -> str:
print("From inside function.")
return 'Hello ' + name
As expected, it works when a string is sent as argument. However, when an integer is used as an argument, it still reaches inside the function and there is error thrown at return
statement level:
> print(greeting(55))
From inside function.
Traceback (most recent call last):
File "testing.py", line 16, in <module>
print(greeting(55))
File "testing.py", line 13, in greeting
return 'Hello ' + name
TypeError: Can't convert 'int' object to str implicitly
Same error occurs if the function definition does not have type specification:
def greeting(name):
Then what is the advantage of having type specification in function header?
def greeting(name: str) -> str: