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
>>> try: ... raise ExceptionGroup("eg", ... [ValueError(1), TypeError(2), OSError(3), OSError(4)]) ... except* TypeError as e: ... print(f'caught {type(e)} with nested {e.exceptions}') ... except* OSError as e: ... print(f'caught {type(e)} with nested {e.exceptions}') ... caught <class 'ExceptionGroup'> with nested (TypeError(2),) caught <class 'ExceptionGroup'> with nested (OSError(3), OSError(4)) + Exception Group Traceback (most recent call last): | File "<doctest default[0]>", line 2, in <module> | raise ExceptionGroup("eg", | [ValueError(1), TypeError(2), OSError(3), OSError(4)]) | ExceptionGroup: eg (1 sub-exception) +-+---------------- 1 ---------------- | ValueError: 1 +------------------------------------
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.
A class definition is an executable statement. The inheritance list usually gives a list of base classes (see :ref:`metaclasses` for more advanced uses), so each item in the list should evaluate to a class object which allows subclassing. Classes without an inheritance list inherit, by default, from the base class :class:`object`; hence, ::
**Programmer's note:** Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with ``self.name = value``. Both class and instance attributes are accessible through the notation "``self.name``", and an instance attribute hides a class attribute with the same name when accessed in this way. Class attributes can be used as defaults for instance attributes, but using mutable values there can lead to unexpected results. :ref:`Descriptors <descriptors>` can be used to create instance variables with different implementation details.
Support for default values was added (see :pep:`696`).
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`.
def overly_generic[ SimpleTypeVar, TypeVarWithBound: int, TypeVarWithConstraints: (str, bytes), TypeVarWithDefault = int, *SimpleTypeVarTuple = (int, float), **SimpleParamSpec = (str, bytearray), ]( a: SimpleTypeVar, b: TypeVarWithDefault, c: TypeVarWithBound, d: Callable[SimpleParamSpec, TypeVarWithConstraints], *e: SimpleTypeVarTuple, ): ...
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.
@decorator def func[T: int, *Ts, **P](*args: *Ts, arg: Callable[P, T] = some_default): ...
DEFAULT_OF_arg = some_default annotation-def TYPE_PARAMS_OF_func(): annotation-def BOUND_OF_T(): return int # In reality, BOUND_OF_T() is evaluated only on demand. T = typing.TypeVar("T", bound=BOUND_OF_T()) Ts = typing.TypeVarTuple("Ts") P = typing.ParamSpec("P") def func(*args: *Ts, arg: Callable[P, T] = DEFAULT_OF_arg): ... func.__type_params__ = (T, Ts, P) return func func = decorator(TYPE_PARAMS_OF_func())
The capitalized names like ``DEFAULT_OF_arg`` are not actually bound at runtime.
Annotations are now lazily evaluated by default.
By default, annotations are lazily evaluated in an :ref:`annotation scope <annotation-scopes>`. This means that they are not evaluated when the code containing the annotation is evaluated. Instead, the interpreter saves information that can be used to evaluate the annotation later if requested. The :mod:`annotationlib` module provides tools for evaluating annotations.
default
bawaan