Skip to content

[3.13] gh-120661: improve example for basic type hints (GH-120934) #120987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ This module provides runtime support for type hints.

Consider the function below::

def moon_weight(earth_weight: float) -> str:
return f'On the moon, you would weigh {earth_weight * 0.166} kilograms.'
def surface_area_of_cube(edge_length: float) -> str:
return f"The surface area of the cube is {6 * edge_length ** 2}."

The function ``moon_weight`` takes an argument expected to be an instance of :class:`float`,
as indicated by the *type hint* ``earth_weight: float``. The function is expected to
return an instance of :class:`str`, as indicated by the ``-> str`` hint.
The function ``surface_area_of_cube`` takes an argument expected to
be an instance of :class:`float`, as indicated by the :term:`type hint`
``edge_length: float``. The function is expected to return an instance
of :class:`str`, as indicated by the ``-> str`` hint.

While type hints can be simple classes like :class:`float` or :class:`str`,
they can also be more complex. The :mod:`typing` module provides a vocabulary of
Expand Down
Loading