ID EN
Typing

ParamSpec

Python 3.11 🇮🇩 Bahasa Indonesia

Variabel spesifikasi parameter. Versi khusus dari variabel tipe.

Syntax

PYTHON
class typing.ParamSpec(name, *, bound=None, covariant=False, contravariant=False)

Contoh

Example 1
PYTHON
P = ParamSpec('P')
Example 2
PYTHON
from collections.abc import Callable
from typing import TypeVar, ParamSpec
import logging

T = TypeVar('T')
P = ParamSpec('P')

def add_logging(f: Callable[P, T]) -> Callable[P, T]:
    '''A type-safe decorator to add logging to a function.'''
    def inner(*args: P.args, **kwargs: P.kwargs) -> T:
        logging.info(f'{f.__name__} was called')
        return f(*args, **kwargs)
    return inner

@add_logging
def add_two(x: float, y: float) -> float:
    '''Add two numbers together.'''
    return x + y

See Also

type variables Generic TypeVar Any cast() ParamSpecArgs ParamSpecKwargs TypeVar