mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
[3.12] gh-120661: improve example for basic type hints (GH-120934) (#120988)
gh-120661: improve example for basic type hints (GH-120934)
(cherry picked from commit bb057ea107
)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
433a5f89c9
commit
566898a3f6
1 changed files with 6 additions and 5 deletions
|
@ -27,12 +27,13 @@ This module provides runtime support for type hints.
|
||||||
|
|
||||||
Consider the function below::
|
Consider the function below::
|
||||||
|
|
||||||
def moon_weight(earth_weight: float) -> str:
|
def surface_area_of_cube(edge_length: float) -> str:
|
||||||
return f'On the moon, you would weigh {earth_weight * 0.166} kilograms.'
|
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`,
|
The function ``surface_area_of_cube`` takes an argument expected to
|
||||||
as indicated by the *type hint* ``earth_weight: float``. The function is expected to
|
be an instance of :class:`float`, as indicated by the :term:`type hint`
|
||||||
return an instance of :class:`str`, as indicated by the ``-> str`` 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`,
|
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
|
they can also be more complex. The :mod:`typing` module provides a vocabulary of
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue