Pelokalan
Python ID
id en
Masuk
Proyek
reference__compound_stmts.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
A list of :ref:`type parameters <type-params>` may be given in square brackets between the function's name and the opening parenthesis for its parameter list. This indicates to static type checkers that the function is generic. At runtime, the type parameters can be retrieved from the function's :attr:`~function.__type_params__` attribute. See :ref:`generic-functions` for more.
Type parameter lists are new in Python 3.12.
When one or more :term:`parameters <parameter>` have the form *parameter* ``=`` *expression*, the function is said to have "default parameter values." For a parameter with a default value, the corresponding :term:`argument` may be omitted from a call, in which case the parameter's default value is substituted. If a parameter has a default value, all following parameters up until the "``*``" must also have a default value --- this is a syntactic restriction that is not expressed by the grammar.
**Default parameter values are evaluated from left to right when the function definition is executed.** This means that the expression is evaluated once, when the function is defined, and that the same "pre-computed" value is used for each call. This is especially important to understand when a default parameter value is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default parameter value is in effect modified. This is generally not what was intended. A way around this is to use ``None`` as the default, and explicitly test for it in the body of the function, for example::
Function call semantics are described in more detail in section :ref:`calls`. A function call always assigns values to all parameters mentioned in the parameter list, either from positional arguments, from keyword arguments, or from default values. If the form "``*identifier``" is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form "``**identifier``" is present, it is initialized to a new ordered mapping receiving any excess keyword arguments, defaulting to a new empty mapping of the same type. Parameters after "``*``" or "``*identifier``" are keyword-only parameters and may only be passed by keyword arguments. Parameters before "``/``" are positional-only parameters and may only be passed by positional arguments.
The ``/`` function parameter syntax may be used to indicate positional-only parameters. See :pep:`570` for details.
Parameters may have an :term:`annotation <function annotation>` of the form "``: expression``" following the parameter name. Any parameter may have an annotation, even those of the form ``*identifier`` or ``**identifier``. (As a special case, parameters of the form ``*identifier`` may have an annotation "``: *expression``".) Functions may have "return" annotation of the form "``-> expression``" after the parameter list. These annotations can be any valid Python expression. The presence of annotations does not change the semantics of a function. See :ref:`annotations` for more information on annotations.
Parameters of the form "``*identifier``" may have an annotation "``: *expression``". See :pep:`646`.
A list of :ref:`type parameters <type-params>` may be given in square brackets immediately after the class's name. This indicates to static type checkers that the class is generic. At runtime, the type parameters can be retrieved from the class's :attr:`~type.__type_params__` attribute. See :ref:`generic-classes` for more.
Type parameter lists
:ref:`Functions <def>` (including :ref:`coroutines <async def>`), :ref:`classes <class>` and :ref:`type aliases <type>` may contain a type parameter list::
Type parameters are declared in square brackets (``[]``) immediately after the name of the function, class, or type alias. The type parameters are accessible within the scope of the generic object, but not elsewhere. Thus, after a declaration ``def func[T](): pass``, the name ``T`` is not available in the module scope. Below, the semantics of generic objects are described with more precision. The scope of type parameters is modeled with a special function (technically, an :ref:`annotation scope <annotation-scopes>`) that wraps the creation of the generic object.
Generic functions, classes, and type aliases have a :attr:`~definition.__type_params__` attribute listing their type parameters.
Type parameters come in three kinds:
:data:`typing.ParamSpec`, introduced by a name prefixed with two asterisks (e.g., ``**P``). Semantically, this stands for the parameters of a callable.
For :data:`!typing.TypeVar`\ s declared using the type parameter list syntax, the bound and constraints are not evaluated when the generic object is created, but only when the value is explicitly accessed through the attributes ``__bound__`` and ``__constraints__``. To accomplish this, the bounds or constraints are evaluated in a separate :ref:`annotation scope <annotation-scopes>`.
All three flavors of type parameters can also have a *default value*, which is used when the type parameter is not explicitly provided. This is added by appending a single equals sign (``=``) followed by an expression. Like the bounds and constraints of type variables, the default value is not evaluated when the object is created, but only when the type parameter's ``__default__`` attribute is accessed. To this end, the default value is evaluated in a separate :ref:`annotation scope <annotation-scopes>`. If no default value is specified for a type parameter, the ``__default__`` attribute is set to the special sentinel object :data:`typing.NoDefault`.
The following example indicates the full set of allowed type parameter declarations::
The annotations of generic functions are evaluated within the annotation scope used for declaring the type parameters, but the function's defaults and decorators are not.
The following example illustrates the scoping rules for these cases, as well as for additional flavors of type parameters::
Generic classes implicitly inherit from :data:`typing.Generic`. The base classes and keyword arguments of generic classes are evaluated within the type scope for the type parameters, and decorators are evaluated outside that scope. This is illustrated by this example::
Variables and function parameters may carry :term:`annotations <annotation>`, created by adding a colon after the name, followed by an expression::
parameter
parameter
parameter list
type parameters