In this lesson, you’ll learn about type hinting in Python. Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5.
Here’s an example of adding type information to a function. You annotate the arguments and the return value:
def greet(name: str) -> str:
return "Hello, " + name
The name: str
syntax indicates the name
argument should be of type str
. The ->
syntax indicates the greet()
function will return a string.
The following example function turns a text string into a headline by adding proper capitalization and a decorative line:
Pygator on Nov. 3, 2019
I get that this may have it’s use, but i’ve seen other coding styles that imply what the arguments should be based on the docstring. I think that is another good alternative.