Gợi ý kiểu lambda Python

Thời gian chạy Python không thực thi chức năng và chú thích loại biến. They can be used by third party tools such as type checkers, IDEs, linters, etc

This module provides runtime support for type hints. The most fundamental support consists of the types

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
27,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29, and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30. For a full specification, please see PEP 484. For a simplified introduction to type hints, see PEP 483

The function below takes and returns a string and is annotated as follows

def greeting[name: str] -> str:
    return 'Hello ' + name

In the function

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
31, the argument
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
32 is expected to be of type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33 and the return type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33. Subtypes are accepted as arguments

New features are frequently added to the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 module. The typing_extensions package provides backports of these new features to older versions of Python

For a summary of deprecated features and a deprecation timeline, please see Deprecation Timeline of Major Features

See also

The documentation at https. //typing. readthedocs. io/ serves as useful reference for type system features, useful typing related tools and typing best practices

Relevant PEPs¶

Since the initial introduction of type hints in PEP 484 and PEP 483, a number of PEPs have modified and enhanced Python’s framework for type annotations. These include

  • PEP 526. Syntax for Variable Annotations

    Introducing syntax for annotating variables outside of function definitions, and

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    36

  • PEP 544. Protocols. Structural subtyping [static duck typing]

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    37 and the
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    38 decorator

  • PEP 585. Type Hinting Generics In Standard Collections

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    39 and the ability to use standard library classes as generic types

  • PEP 586. Literal Types

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    40

  • PEP 589. đã gõDict. Nhập gợi ý cho từ điển với một bộ phím cố định

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    41

  • PEP 591. Thêm một vòng loại cuối cùng để gõ

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    42 và trang trí
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    43

  • PEP 593. Chức năng linh hoạt và chú thích biến

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44

  • PEP 604. Cho phép viết các kiểu kết hợp là
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    45

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    46 và khả năng sử dụng toán tử nhị phân hoặc toán tử
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    47 để biểu thị một kết hợp các loại

  • PEP 612. Tham số Thông số kỹ thuật Biến

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    48 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    49

  • PEP 613. Bí danh loại rõ ràng

    Giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    50

  • PEP 646. Variadic Generics

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    51

  • PEP 647. User-Defined Type Guards

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    52

  • PEP 655. Marking individual TypedDict items as required or potentially missing

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    53 and
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    54

  • PEP 673. Self type

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    55

  • PEP 675. Arbitrary Literal String Type

    Introducing

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    56

  • PEP 681. Data Class Transforms

    Introducing the

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    57 decorator

Type aliases¶

A type alias is defined by assigning the type to the alias. In this example,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
58 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
59 will be treated as interchangeable synonyms

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]

Type aliases are useful for simplifying complex type signatures. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
6

Note that

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60 as a type hint is a special case and is replaced by
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
61

NewType¶

Use the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62 helper to create distinct types

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
0

The static type checker will treat the new type as if it were a subclass of the original type. This is useful in helping catch logical errors

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
1

You may still perform all

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63 operations on a variable of type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
64, but the result will always be of type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63. This lets you pass in a
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
64 wherever an
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63 might be expected, but will prevent you from accidentally creating a
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
64 in an invalid way

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
8

Note that these checks are enforced only by the static type checker. At runtime, the statement

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
69 will make
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
70 a callable that immediately returns whatever parameter you pass it. That means the expression
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
71 does not create a new class or introduce much overhead beyond that of a regular function call

More precisely, the expression

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
72 is always true at runtime

It is invalid to create a subtype of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
70

def greeting[name: str] -> str:
    return 'Hello ' + name
4

However, it is possible to create a

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62 based on a ‘derived’
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62

def greeting[name: str] -> str:
    return 'Hello ' + name
7

and typechecking for

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
76 will work as expected

Xem PEP 484 để biết thêm chi tiết

Note

Recall that the use of a type alias declares two types to be equivalent to one another. Doing

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
77 will make the static type checker treat
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
78 as being exactly equivalent to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
79 in all cases. This is useful when you want to simplify complex type signatures

In contrast,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62 declares one type to be a subtype of another. Doing
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
81 will make the static type checker treat
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
70 as a subclass of
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
79, which means a value of type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
79 cannot be used in places where a value of type
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
70 is expected. Điều này hữu ích khi bạn muốn ngăn ngừa các lỗi logic với chi phí thời gian chạy tối thiểu

Mới trong phiên bản 3. 5. 2

Đã thay đổi trong phiên bản 3. 10. ______062 bây giờ là một lớp chứ không phải là một hàm. Có một số chi phí thời gian chạy bổ sung khi gọi

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62 qua một chức năng thông thường. Tuy nhiên, chi phí này sẽ giảm trong 3. 11. 0.

Có thể gọi¶

Các khung mong đợi các chức năng gọi lại của các chữ ký cụ thể có thể được gợi ý bằng cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
88

Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
1

Có thể khai báo kiểu trả về của một hàm có thể gọi được mà không chỉ định chữ ký cuộc gọi bằng cách thay thế dấu chấm lửng bằng chữ cho danh sách các đối số trong gợi ý kiểu.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
89

Các khả năng gọi được lấy các khả năng gọi khác làm đối số có thể chỉ ra rằng các loại tham số của chúng phụ thuộc vào nhau bằng cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48. Ngoài ra, nếu khả năng gọi đó thêm hoặc xóa đối số khỏi các khả năng gọi khác, toán tử
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 có thể được sử dụng. Chúng có dạng tương ứng là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
92 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
93

Đã thay đổi trong phiên bản 3. 10. ______028 hiện hỗ trợ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49. Xem PEP 612 để biết thêm chi tiết.

See also

Tài liệu về

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 cung cấp các ví dụ về cách sử dụng trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28

Thuốc generic¶

Vì thông tin loại về các đối tượng được giữ trong vùng chứa không thể được suy luận tĩnh theo cách chung chung, nên các lớp cơ sở trừu tượng đã được mở rộng để hỗ trợ đăng ký biểu thị các loại dự kiến ​​cho các phần tử vùng chứa

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
3

Generics có thể được tham số hóa bằng cách sử dụng một nhà máy có sẵn trong cách gõ có tên là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
0

Loại chung do người dùng định nghĩa¶

Một lớp do người dùng định nghĩa có thể được định nghĩa là một lớp chung

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
1

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
601 với tư cách là một lớp cơ sở định nghĩa rằng lớp
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
602 nhận một tham số kiểu duy nhất là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603. Điều này cũng làm cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603 hợp lệ như một kiểu trong thân lớp

Lớp cơ sở

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30 định nghĩa
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
606 để
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
607 có giá trị như một loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
2

Một loại chung có thể có bất kỳ số lượng biến loại nào. Tất cả các loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29 đều được phép làm tham số cho một loại chung

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
3

Mỗi đối số biến loại cho

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30 phải khác biệt. Điều này là không hợp lệ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
4

Bạn có thể sử dụng đa thừa kế với

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
5

Khi kế thừa từ các lớp chung, một số biến kiểu có thể được sửa

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
6

Trong trường hợp này,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
611 có một tham số duy nhất,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603

Sử dụng một lớp chung mà không chỉ định các tham số loại giả sử

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 cho mỗi vị trí. Trong ví dụ sau,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
614 không phải là chung chung nhưng hoàn toàn kế thừa từ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
615

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
7

Bí danh loại chung do người dùng xác định cũng được hỗ trợ. ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
8

Đã thay đổi trong phiên bản 3. 7. ______030 không còn có siêu dữ liệu tùy chỉnh.

Generics do người dùng định nghĩa cho các biểu thức tham số cũng được hỗ trợ thông qua các biến đặc tả tham số ở dạng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
617. Hành vi phù hợp với các biến loại' được mô tả ở trên vì các biến đặc tả tham số được mô-đun đánh máy coi là một biến loại chuyên biệt. Một ngoại lệ cho điều này là một danh sách các loại có thể được sử dụng để thay thế một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
9

Hơn nữa, một biến chung chỉ có một biến đặc tả tham số sẽ chấp nhận danh sách tham số ở dạng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
619 và cả
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
620 vì lý do thẩm mỹ. Trong nội bộ, cái sau được chuyển đổi thành cái trước, vì vậy những điều sau đây là tương đương

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60

Xin lưu ý rằng thuốc generic có

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 có thể không có
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
622 chính xác sau khi thay thế trong một số trường hợp vì chúng chủ yếu dành cho kiểm tra loại tĩnh

Đã thay đổi trong phiên bản 3. 10. ______030 hiện có thể được tham số hóa qua các biểu thức tham số. Xem

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 và PEP 612 để biết thêm chi tiết.

Lớp chung do người dùng định nghĩa có thể có ABC làm lớp cơ sở mà không có xung đột siêu dữ liệu. Siêu dữ liệu chung không được hỗ trợ. Kết quả của việc tham số hóa các khái quát được lưu vào bộ đệm và hầu hết các loại trong mô-đun gõ đều có thể băm và có thể so sánh bằng nhau

Loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26¶

Một loại đặc biệt của loại là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26. Trình kiểm tra loại tĩnh sẽ coi mọi loại là tương thích với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 là tương thích với mọi loại

Điều này có nghĩa là có thể thực hiện bất kỳ thao tác hoặc lệnh gọi phương thức nào trên một giá trị kiểu

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 và gán nó cho bất kỳ biến nào

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
61

Lưu ý rằng không có kiểm tra loại nào được thực hiện khi gán giá trị loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 cho loại chính xác hơn. Ví dụ: trình kiểm tra kiểu tĩnh không báo lỗi khi gán
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
631 cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
632 mặc dù
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
632 được khai báo là kiểu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33 và nhận giá trị
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63 khi chạy

Hơn nữa, tất cả các hàm không có kiểu trả về hoặc kiểu tham số sẽ mặc định mặc định sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
62

Hành vi này cho phép sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 làm lối thoát hiểm khi bạn cần kết hợp mã được nhập động và mã tĩnh

Đối chiếu hành vi của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 với hành vi của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
639. Tương tự như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26, mỗi loại là một loại con của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
639. Tuy nhiên, không giống như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26, điều ngược lại không đúng.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
639 không phải là kiểu con của mọi kiểu khác

Điều đó có nghĩa là khi loại của một giá trị là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
639, trình kiểm tra loại sẽ từ chối hầu hết tất cả các hoạt động trên nó và gán nó cho một biến [hoặc sử dụng nó làm giá trị trả về] của loại chuyên biệt hơn là lỗi loại. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63

Sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
639 để chỉ ra rằng một giá trị có thể là bất kỳ loại nào theo cách an toàn về kiểu. Sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 để chỉ ra rằng một giá trị được nhập động

Phân nhóm danh nghĩa và phân nhóm cấu trúc¶

Ban đầu, PEP 484 đã định nghĩa hệ thống kiểu tĩnh Python là sử dụng kiểu con danh nghĩa. Điều này có nghĩa là lớp

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
647 được cho phép khi lớp
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
648 được mong đợi nếu và chỉ khi lớp
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
647 là lớp con của lớp
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
648

Yêu cầu này trước đây cũng được áp dụng cho các lớp cơ sở trừu tượng, chẳng hạn như

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
651. Vấn đề với cách tiếp cận này là một lớp phải được đánh dấu rõ ràng để hỗ trợ chúng, điều này không phổ biến và không giống như những gì người ta thường làm trong mã Python được gõ động thành ngữ. Ví dụ: điều này phù hợp với PEP 484

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
64

PEP 544 cho phép giải quyết vấn đề này bằng cách cho phép người dùng viết đoạn mã trên mà không có lớp cơ sở rõ ràng trong định nghĩa lớp, cho phép

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
652 được coi là kiểu con của cả
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
653 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
654 bằng trình kiểm tra kiểu tĩnh. Điều này được gọi là phân nhóm cấu trúc [hoặc phân nhóm tĩnh]

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
65

Hơn nữa, bằng cách phân lớp một lớp đặc biệt

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
37, người dùng có thể xác định các giao thức tùy chỉnh mới để tận hưởng đầy đủ phân loại cấu trúc [xem ví dụ bên dưới]

Nội dung học phần¶

Mô-đun định nghĩa các lớp, hàm và trình trang trí sau

Note

Mô-đun này xác định một số loại là các lớp con của các lớp thư viện tiêu chuẩn đã tồn tại từ trước, lớp này cũng mở rộng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30 để hỗ trợ các biến loại bên trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657. Các loại này trở nên dư thừa trong Python 3. 9 khi các lớp có sẵn tương ứng được tăng cường để hỗ trợ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657

Các loại dự phòng không được dùng nữa kể từ Python 3. 9 nhưng trình thông dịch sẽ không đưa ra cảnh báo phản đối nào. Dự kiến, trình kiểm tra loại sẽ gắn cờ các loại không dùng nữa khi chương trình được kiểm tra nhắm mục tiêu Python 3. 9 hoặc mới hơn

The deprecated types will be removed from the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 module in the first Python version released 5 years after the release of Python 3. 9. 0. Xem chi tiết trong PEP 585—Type Gợi ý Generics Trong Bộ sưu tập Tiêu chuẩn

Kiểu gõ đặc biệt¶

Các loại đặc biệt¶

Chúng có thể được sử dụng làm loại trong chú thích và không hỗ trợ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657

đang gõ. Bất kỳ

Loại đặc biệt cho biết loại không bị ràng buộc

  • Mọi loại đều tương thích với

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    26

  • Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    26 tương thích với mọi loại

Đã thay đổi trong phiên bản 3. 11. ______026 hiện có thể được sử dụng làm lớp cơ sở. Điều này có thể hữu ích để tránh các lỗi trình kiểm tra kiểu với các lớp có thể gõ ở bất cứ đâu hoặc rất năng động.

đang gõ. Chuỗi ký tự

Loại đặc biệt chỉ bao gồm các chuỗi ký tự. Một chuỗi ký tự tương thích với

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
56, cũng như một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
56 khác, nhưng một đối tượng được nhập là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33 thì không. Một chuỗi được tạo bằng cách soạn các đối tượng kiểu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
56 cũng được chấp nhận là một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
56

Thí dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
66

Điều này hữu ích cho các API nhạy cảm nơi các chuỗi tùy ý do người dùng tạo có thể gây ra sự cố. Ví dụ: hai trường hợp trên tạo ra lỗi trình kiểm tra kiểu có thể dễ bị tấn công SQL injection

Xem PEP 675 để biết thêm chi tiết

Mới trong phiên bản 3. 11

đang gõ. Không bao giờ

Loại dưới cùng, một loại không có thành viên

Điều này có thể được sử dụng để xác định một hàm không bao giờ được gọi hoặc một hàm không bao giờ trả về

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
67

Mới trong phiên bản 3. 11. Trên các phiên bản Python cũ hơn,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
669 có thể được sử dụng để diễn đạt cùng một khái niệm.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
670 đã được thêm vào để làm cho ý nghĩa rõ ràng hơn.

đang gõ. NoReturn

Loại đặc biệt chỉ ra rằng một hàm không bao giờ trả về. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
68

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
669 cũng có thể được sử dụng làm loại dưới cùng, loại không có giá trị. Bắt đầu bằng Python 3. 11, thay vào đó nên sử dụng loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
670 cho khái niệm này. Người kiểm tra loại nên đối xử với cả hai như nhau

Mới trong phiên bản 3. 5. 4

Mới trong phiên bản 3. 6. 2

đang gõ. Bản thân

Loại đặc biệt để đại diện cho lớp kèm theo hiện tại. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
69

Chú thích này về mặt ngữ nghĩa tương đương với chú thích sau, mặc dù theo cách ngắn gọn hơn

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
00

In general if something currently follows the pattern of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
01

Bạn nên sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
55 vì các lệnh gọi tới
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
674 sẽ có kiểu trả về là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
675 chứ không phải
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
676

Các trường hợp sử dụng phổ biến khác bao gồm

  • Các

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    677 được sử dụng làm hàm tạo thay thế và trả về các thể hiện của tham số
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    678

  • Chú thích một phương thức

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    679 trả về self

Xem PEP 673 để biết thêm chi tiết

Mới trong phiên bản 3. 11

đang gõ. TypeAlias

Chú thích đặc biệt để khai báo rõ ràng một loại bí danh . Ví dụ.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
02

Xem PEP 613 để biết thêm chi tiết về bí danh loại rõ ràng

Mới trong phiên bản 3. 10

Biểu mẫu đặc biệt¶

Chúng có thể được sử dụng làm loại trong chú thích bằng cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657, mỗi loại có một cú pháp duy nhất

đang gõ. Tuple

Loại tuple; . Loại của bộ dữ liệu trống có thể được viết là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
682

Thí dụ.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
683 là một bộ gồm hai phần tử tương ứng với các biến kiểu T1 và T2.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
684 là một bộ gồm một số nguyên, một số float và một chuỗi

Để chỉ định một bộ có độ dài thay đổi thuộc loại đồng nhất, hãy sử dụng dấu chấm lửng bằng chữ, e. g.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
685. Một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
686 đơn giản tương đương với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
687 và lần lượt là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
688

Không dùng nữa kể từ phiên bản 3. 9. ______3689 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

đang gõ. Liên minh

loại hình công đoàn;

Để xác định một công đoàn, sử dụng e. g.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
693 hoặc viết tắt là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
694. Sử dụng tốc ký đó được khuyến khích. Thông tin chi tiết

  • Các đối số phải là các loại và phải có ít nhất một

  • Liên minh công đoàn bị san phẳng, đ. g

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    03

  • Liên minh của một đối số duy nhất biến mất, e. g

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    04

  • Đối số dư thừa được bỏ qua, e. g

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    05

  • Khi so sánh các công đoàn, thứ tự đối số bị bỏ qua, e. g

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    06

  • Bạn không thể phân lớp hoặc khởi tạo một

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    27

  • Bạn không thể viết

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    696

Đã thay đổi trong phiên bản 3. 7. Không xóa các lớp con rõ ràng khỏi liên kết trong thời gian chạy.

Đã thay đổi trong phiên bản 3. 10. Các hiệp hội hiện có thể được viết là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
45. Xem biểu thức loại liên kết .

đang gõ. Optional

loại tùy chọn

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
698 tương đương với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
699 [hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
000]

Lưu ý rằng đây không phải là khái niệm giống như đối số tùy chọn, là đối số có giá trị mặc định. Đối số tùy chọn với giá trị mặc định không yêu cầu vòng loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
001 trên chú thích loại của nó chỉ vì nó là tùy chọn. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
07

Mặt khác, nếu giá trị rõ ràng của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60 được cho phép, thì việc sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
001 là phù hợp, cho dù đối số có phải là tùy chọn hay không. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
08

Đã thay đổi trong phiên bản 3. 10. Tùy chọn bây giờ có thể được viết là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
699. Xem biểu thức loại liên kết .

đang gõ. Có thể gọi được

Loại có thể gọi được;

Cú pháp đăng ký phải luôn được sử dụng với chính xác hai giá trị. danh sách đối số và kiểu trả về. Danh sách đối số phải là một danh sách các loại hoặc dấu chấm lửng;

Không có cú pháp để chỉ ra các đối số tùy chọn hoặc từ khóa; .

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
89 [dấu chấm lửng theo nghĩa đen] có thể được sử dụng để nhập gợi ý có thể gọi được, lấy bất kỳ số lượng đối số nào và trả về
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
007. Một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28 đơn giản tương đương với ________ 4009 và lần lượt là ________ 4010

Các khả năng gọi được lấy các khả năng gọi khác làm đối số có thể chỉ ra rằng các loại tham số của chúng phụ thuộc vào nhau bằng cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48. Ngoài ra, nếu khả năng gọi đó thêm hoặc xóa đối số khỏi các khả năng gọi khác, toán tử
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 có thể được sử dụng. Chúng có dạng tương ứng là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
92 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
93

Không dùng nữa kể từ phiên bản 3. 9. ______4010 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

Đã thay đổi trong phiên bản 3. 10. ______028 hiện hỗ trợ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49. Xem PEP 612 để biết thêm chi tiết.

See also

Tài liệu về

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 cung cấp các ví dụ về cách sử dụng với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28

đang gõ. Nối

Được sử dụng với

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 để nhập chú thích cho một thứ có thể gọi được cao hơn để thêm, xóa hoặc chuyển đổi các tham số của một thứ có thể gọi khác. Cách sử dụng có dạng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
025.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 hiện chỉ hợp lệ khi được sử dụng làm đối số đầu tiên cho một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28. Tham số cuối cùng của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 phải là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 hoặc dấu chấm lửng [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
030]

Ví dụ: để chú thích một trình trang trí

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
031 cung cấp một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
032 cho chức năng được trang trí, có thể sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 để chỉ ra rằng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
031 mong đợi một hàm có thể gọi được lấy trong một đối số đầu tiên là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
035 và trả về một hàm có thể gọi được với một chữ ký loại khác. Trong trường hợp này,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 chỉ ra rằng các loại tham số của hàm có thể gọi được trả về phụ thuộc vào các loại tham số của hàm có thể gọi được truyền vào

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
09

Mới trong phiên bản 3. 10

See also

  • PEP 612 – Biến thông số kỹ thuật tham số [PEP đã giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    48 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    49]

  • Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    48 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    28

lớp đang gõ. Loại[Chung[CT_co]]

Một biến được chú thích bằng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041 có thể chấp nhận giá trị loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041. Ngược lại, một biến được chú thích bằng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
043 có thể chấp nhận các giá trị là chính các lớp – cụ thể, nó sẽ chấp nhận đối tượng lớp của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
10

Lưu ý rằng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
043 là hiệp phương sai

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
11

Thực tế là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
043 là biến thể ngụ ý rằng tất cả các lớp con của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041 nên triển khai cùng chữ ký hàm tạo và chữ ký phương thức lớp như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041. Trình kiểm tra loại sẽ gắn cờ vi phạm điều này, nhưng cũng nên cho phép các lệnh gọi hàm tạo trong các lớp con khớp với các lệnh gọi hàm tạo trong lớp cơ sở được chỉ định. Cách trình kiểm tra loại được yêu cầu để xử lý trường hợp cụ thể này có thể thay đổi trong các phiên bản PEP 484 trong tương lai

Các tham số pháp lý duy nhất cho

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
049 là các lớp,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26, biến loại và liên kết của bất kỳ loại nào trong số này. Ví dụ.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
12

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
051 tương đương với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
049, đến lượt nó tương đương với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
053, là gốc của hệ thống phân cấp siêu dữ liệu của Python

Mới trong phiên bản 3. 5. 2

Không dùng nữa kể từ phiên bản 3. 9. ______4054 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

đang gõ. Chữ

Loại có thể được sử dụng để chỉ báo cho người kiểm tra loại rằng tham số hàm hoặc biến tương ứng có giá trị tương đương với chữ được cung cấp [hoặc một trong số nhiều chữ]. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
13

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
056 không thể được phân lớp. Trong thời gian chạy, một giá trị tùy ý được phép làm đối số kiểu cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
056, nhưng trình kiểm tra kiểu có thể áp đặt các hạn chế. Xem PEP 586 để biết thêm chi tiết về các loại chữ

Mới trong phiên bản 3. 8

Đã thay đổi trong phiên bản 3. 9. 1. ______040 hiện loại bỏ các tham số trùng lặp. So sánh bình đẳng của các đối tượng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
40 không còn phụ thuộc vào thứ tự. Các đối tượng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
40 bây giờ sẽ đưa ra một ngoại lệ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
061 trong quá trình so sánh bằng nếu một trong các tham số của chúng không có thể băm .

đang gõ. ClassVar

Cấu trúc kiểu đặc biệt để đánh dấu các biến lớp

Như đã giới thiệu trong PEP 526, một chú thích biến được bao bọc trong ClassVar cho biết rằng một thuộc tính nhất định được dự định sử dụng làm biến lớp và không được đặt trên các phiên bản của lớp đó. Cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
14

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
36 chỉ chấp nhận các loại và không thể đăng ký thêm

Bản thân

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
36 không phải là một lớp và không được sử dụng với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
064 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
065.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
36 không thay đổi hành vi thời gian chạy Python, nhưng nó có thể được sử dụng bởi trình kiểm tra loại của bên thứ ba. Ví dụ: trình kiểm tra loại có thể gắn cờ mã sau đây là lỗi

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
15

Mới trong phiên bản 3. 5. 3

đang gõ. Cuối cùng

Một cấu trúc gõ đặc biệt để chỉ ra cho người kiểm tra loại rằng một tên không thể được gán lại hoặc ghi đè trong một lớp con. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
16

Không có kiểm tra thời gian chạy của các thuộc tính này. Xem PEP 591 để biết thêm chi tiết

Mới trong phiên bản 3. 8

đang gõ. Bắt buộcđang gõ. Không bắt buộc

Các cấu trúc gõ đặc biệt đánh dấu các phím riêng lẻ của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 là bắt buộc hoặc không bắt buộc tương ứng

Xem

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 và PEP 655 để biết thêm chi tiết

Mới trong phiên bản 3. 11

đang gõ. Chú thích

Một loại, được giới thiệu trong PEP 593 [_______4069], để trang trí các loại hiện có với siêu dữ liệu theo ngữ cảnh cụ thể [có thể là nhiều phần của nó, vì

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 là biến thể]. Cụ thể, loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603 có thể được chú thích bằng siêu dữ liệu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
072 thông qua gợi ý đánh máy
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
073. Siêu dữ liệu này có thể được sử dụng cho phân tích tĩnh hoặc trong thời gian chạy. Nếu một thư viện [hoặc công cụ] gặp một gợi ý đánh máy
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
073 và không có logic đặc biệt nào cho siêu dữ liệu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
072, thì thư viện đó sẽ bỏ qua nó và chỉ coi loại đó là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603. Không giống như chức năng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
077 hiện có trong mô-đun
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 vô hiệu hóa hoàn toàn các chú thích kiểm tra đánh máy trên một hàm hoặc một lớp, loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 cho phép cả kiểm tra đánh máy tĩnh của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603 [có thể bỏ qua
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
072 một cách an toàn] cùng với quyền truy cập thời gian chạy vào
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
072 trong một ứng dụng cụ thể

Cuối cùng, trách nhiệm về cách giải thích các chú thích [nếu có] là trách nhiệm của công cụ hoặc thư viện gặp phải loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44. Một công cụ hoặc thư viện gặp loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 có thể quét qua các chú thích để xác định xem chúng có đáng quan tâm hay không [e. g. , using
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
064]

Khi một công cụ hoặc thư viện không hỗ trợ chú thích hoặc gặp một chú thích không xác định, nó chỉ nên bỏ qua nó và coi loại chú thích là loại cơ bản

Tùy thuộc vào công cụ sử dụng các chú thích để quyết định xem máy khách có được phép có nhiều chú thích trên một loại hay không và cách hợp nhất các chú thích đó

Vì loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 cho phép bạn đặt một số chú thích cùng [hoặc khác] loại trên bất kỳ nút nào, các công cụ hoặc thư viện sử dụng các chú thích đó chịu trách nhiệm xử lý các bản sao tiềm năng. Ví dụ: nếu bạn đang thực hiện phân tích phạm vi giá trị, bạn có thể cho phép điều này

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
17

Chuyển

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
087 đến
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
088 cho phép một người truy cập các chú thích bổ sung khi chạy

Chi tiết cú pháp

  • Đối số đầu tiên của

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44 phải là loại hợp lệ

  • Nhiều chú thích loại được hỗ trợ [

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44 hỗ trợ các đối số biến đổi]

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    18

  • Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44 phải được gọi với ít nhất hai đối số [
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    092 không hợp lệ]

  • Thứ tự của các chú thích được giữ nguyên và các vấn đề cần kiểm tra bằng

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    19

  • Các loại

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44 lồng nhau được làm phẳng, với siêu dữ liệu được sắp xếp bắt đầu bằng chú thích trong cùng

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    80

  • Chú thích trùng lặp không bị xóa

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    81

  • Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    44 có thể được sử dụng với các bí danh lồng nhau và chung chung

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    82

Mới trong phiên bản 3. 9

đang gõ. TypeGuard

Biểu mẫu nhập đặc biệt được sử dụng để chú thích kiểu trả về của hàm bảo vệ kiểu do người dùng xác định.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52 only accepts a single type argument. Khi chạy, các hàm được đánh dấu theo cách này sẽ trả về một giá trị boolean

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52 nhằm mục đích mang lại lợi ích cho việc thu hẹp loại – một kỹ thuật được sử dụng bởi trình kiểm tra loại tĩnh để xác định loại biểu thức chính xác hơn trong luồng mã của chương trình. Thông thường, việc thu hẹp loại được thực hiện bằng cách phân tích luồng mã có điều kiện và áp dụng việc thu hẹp cho một khối mã. Biểu thức điều kiện ở đây đôi khi được gọi là "loại bảo vệ"

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
83

Đôi khi sẽ thuận tiện khi sử dụng hàm boolean do người dùng định nghĩa làm bộ bảo vệ kiểu. Một chức năng như vậy nên sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
097 làm kiểu trả về của nó để cảnh báo những người kiểm tra kiểu tĩnh về ý định này

Sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
098 cho trình kiểm tra kiểu tĩnh biết rằng đối với một chức năng nhất định

  1. Giá trị trả về là một boolean

  2. Nếu giá trị trả về là

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    099, loại đối số của nó là loại bên trong
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    52

Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
84

Nếu

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
101 là một phương thức lớp hoặc thể hiện, thì kiểu trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52 ánh xạ tới kiểu của tham số thứ hai sau
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
678 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
104

Tóm lại, dạng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
105, có nghĩa là nếu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
106 trả về
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099, thì
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
108 thu hẹp từ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
109 thành
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
110

Note

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
110 không nhất thiết phải là dạng hẹp hơn của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
109 – nó thậm chí có thể là dạng rộng hơn. Lý do chính là để cho phép những thứ như thu hẹp
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
113 thành
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
114 mặc dù cái sau không phải là kiểu con của cái trước, vì
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
115 là bất biến. Trách nhiệm viết các bộ bảo vệ loại an toàn thuộc về người dùng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52 cũng hoạt động với các biến kiểu. Xem PEP 647 để biết thêm chi tiết

Mới trong phiên bản 3. 10

Xây dựng các loại chung¶

Chúng không được sử dụng trong chú thích. Họ đang xây dựng các khối để tạo các loại chung

lớp đang gõ. Chung

Lớp cơ sở trừu tượng cho các loại chung

Một kiểu chung thường được khai báo bằng cách kế thừa từ một phần khởi tạo của lớp này với một hoặc nhiều biến kiểu. Ví dụ: loại ánh xạ chung có thể được định nghĩa là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
85

Lớp này sau đó có thể được sử dụng như sau

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
86

lớp đang gõ. TypeVar

Loại biến

Usage

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
87

Biến loại tồn tại chủ yếu vì lợi ích của trình kiểm tra loại tĩnh. Chúng đóng vai trò là tham số cho các kiểu chung cũng như cho các định nghĩa hàm chung. Xem

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30 để biết thêm thông tin về các loại chung. Các chức năng chung hoạt động như sau

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
88

Lưu ý rằng các biến loại có thể bị ràng buộc, bị ràng buộc hoặc không, nhưng không thể vừa bị ràng buộc vừa bị ràng buộc

Biến loại ràng buộc và biến loại ràng buộc có ngữ nghĩa khác nhau theo một số cách quan trọng. Sử dụng biến loại ràng buộc có nghĩa là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29 sẽ được giải quyết bằng cách sử dụng loại cụ thể nhất có thể

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
89

Các biến loại có thể được liên kết với các loại cụ thể, loại trừu tượng [ABC hoặc giao thức] và thậm chí cả các loại kết hợp

def greeting[name: str] -> str:
    return 'Hello ' + name
40

Tuy nhiên, việc sử dụng một biến loại bị ràng buộc có nghĩa là chỉ có thể giải quyết

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29 khi chính xác là một trong những ràng buộc đã cho

def greeting[name: str] -> str:
    return 'Hello ' + name
41

Khi chạy,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
120 sẽ tăng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
061. Nói chung, không nên sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
064 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
065 với các loại

Biến loại có thể được đánh dấu hiệp biến hoặc chống biến bằng cách chuyển

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
124 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
125. Xem PEP 484 để biết thêm chi tiết. Theo mặc định, các biến loại là bất biến

lớp đang gõ. TypeVarTuple

Loại tuple biến. Một dạng chuyên biệt của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
126 cho phép sử dụng chung các biến thể

Biến loại bình thường cho phép tham số hóa với một loại duy nhất. Ngược lại, một bộ biến kiểu cho phép tham số hóa với số kiểu tùy ý bằng cách hành động giống như một số biến kiểu tùy ý được bao bọc trong một bộ. Ví dụ

def greeting[name: str] -> str:
    return 'Hello ' + name
42

Lưu ý việc sử dụng toán tử giải nén

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
127 trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
128. Về mặt khái niệm, bạn có thể nghĩ về
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
129 như một bộ biến kiểu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
130.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
128 sau đó sẽ trở thành
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
132, tương đương với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
133. [Lưu ý rằng trong các phiên bản Python cũ hơn, bạn có thể thấy điều này được viết bằng cách sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
134 thay vì
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
135. ]

Loại bộ dữ liệu biến phải luôn được giải nén. Điều này giúp phân biệt các bộ biến kiểu với các biến kiểu bình thường

def greeting[name: str] -> str:
    return 'Hello ' + name
43

Bộ dữ liệu biến loại có thể được sử dụng trong cùng ngữ cảnh như biến loại bình thường. Ví dụ: trong định nghĩa lớp, đối số và kiểu trả về

def greeting[name: str] -> str:
    return 'Hello ' + name
44

Các bộ dữ liệu biến loại có thể được kết hợp vui vẻ với các biến loại bình thường

def greeting[name: str] -> str:
    return 'Hello ' + name
45

Tuy nhiên, lưu ý rằng nhiều nhất một bộ biến kiểu có thể xuất hiện trong một danh sách duy nhất các đối số kiểu hoặc tham số kiểu

def greeting[name: str] -> str:
    return 'Hello ' + name
46

Cuối cùng, một bộ biến kiểu đã giải nén có thể được sử dụng làm chú thích kiểu của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
136

def greeting[name: str] -> str:
    return 'Hello ' + name
47

Ngược lại với các chú thích không được giải nén của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
136 - e. g.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
138, sẽ xác định rằng tất cả các đối số là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63 -
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
140 cho phép tham chiếu đến các loại đối số riêng lẻ trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
136. Ở đây, điều này cho phép chúng tôi đảm bảo các loại của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
136 được chuyển đến
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
143 khớp với các loại đối số [vị trí] của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
144

Xem PEP 646 để biết thêm chi tiết về các bộ biến loại

Mới trong phiên bản 3. 11

đang gõ. Giải nén

Toán tử đánh máy đánh dấu một đối tượng theo khái niệm là đã được giải nén. Ví dụ: sử dụng toán tử giải nén

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
127 trên
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
146 tương đương với việc sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
134 để đánh dấu bộ biến kiểu là đã được giải nén

def greeting[name: str] -> str:
    return 'Hello ' + name
48

Trên thực tế,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
134 có thể được sử dụng thay thế cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
127 trong ngữ cảnh của các loại. Bạn có thể thấy
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
134 được sử dụng một cách rõ ràng trong các phiên bản Python cũ hơn, trong đó không thể sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
127 ở một số nơi nhất định

def greeting[name: str] -> str:
    return 'Hello ' + name
49

Mới trong phiên bản 3. 11

lớp đang gõ. Thông số kỹ thuật[tên , *, bound=None, covariant=False, contravariant=False]

Thông số kỹ thuật biến. Một phiên bản chuyên biệt của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
152

Usage

def greeting[name: str] -> str:
    return 'Hello ' + name
70

Các biến đặc tả tham số tồn tại chủ yếu vì lợi ích của trình kiểm tra kiểu tĩnh. Chúng được sử dụng để chuyển tiếp các loại tham số của một hàm có thể gọi này sang một hàm có thể gọi khác - một mẫu thường thấy trong các hàm và trình trang trí bậc cao hơn. Chúng chỉ hợp lệ khi được sử dụng trong

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 hoặc làm đối số đầu tiên cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28 hoặc làm tham số cho Generics do người dùng xác định. Xem
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30 để biết thêm thông tin về các loại chung

Ví dụ: để thêm ghi nhật ký cơ bản vào một chức năng, người ta có thể tạo một trình trang trí

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
156 để ghi nhật ký các cuộc gọi chức năng. Biến đặc tả tham số cho trình kiểm tra kiểu biết rằng có thể gọi được chuyển vào trình trang trí và có thể gọi mới được trả về bởi nó có các tham số loại phụ thuộc lẫn nhau

def greeting[name: str] -> str:
    return 'Hello ' + name
71

Không có

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48, cách đơn giản nhất để chú thích điều này trước đây là sử dụng một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29 với ràng buộc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
009. Tuy nhiên điều này gây ra hai vấn đề

  1. Trình kiểm tra loại không thể gõ kiểm tra chức năng

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    160 vì
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    136 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    162 phải được gõ
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    26

  2. Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    164 có thể được yêu cầu trong phần thân của trình trang trí
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    156 khi trả về hàm
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    160 hoặc trình kiểm tra kiểu tĩnh phải được yêu cầu bỏ qua
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    167

argskwargs

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 nắm bắt cả tham số vị trí và từ khóa, nên có thể sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
169 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
170 để tách một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 thành các thành phần của nó.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
169 đại diện cho bộ tham số vị trí trong một cuộc gọi nhất định và chỉ nên được sử dụng để chú thích
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
136.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
170 đại diện cho ánh xạ của các tham số từ khóa với các giá trị của chúng trong một cuộc gọi nhất định và chỉ nên được sử dụng để chú thích
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
162. Cả hai thuộc tính đều yêu cầu tham số được chú thích nằm trong phạm vi. Khi chạy,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
169 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
170 lần lượt là các thể hiện của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
178 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
179

Các biến đặc tả tham số được tạo bằng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
124 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
125 có thể được sử dụng để khai báo các loại chung biến thể hoặc trái ngược. Đối số
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
182 cũng được chấp nhận, tương tự như đối số
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
29. Tuy nhiên, ngữ nghĩa thực tế của những từ khóa này vẫn chưa được quyết định

Mới trong phiên bản 3. 10

Note

Chỉ các biến đặc tả tham số được xác định trong phạm vi toàn cầu mới có thể được chọn

See also

  • PEP 612 – Biến thông số kỹ thuật tham số [PEP đã giới thiệu

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    48 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    49]

  • Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    28 và
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    49

đang gõ. ParamSpecArgsđang gõ. ParamSpecKwargs

Đối số và thuộc tính đối số từ khóa của một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48. Thuộc tính
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
169 của một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 là một thể hiện của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
178, và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
170 là một thể hiện của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
179. Chúng được dành cho nội quan thời gian chạy và không có ý nghĩa đặc biệt đối với trình kiểm tra kiểu tĩnh

Gọi

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
194 trên một trong hai đối tượng này sẽ trả về bản gốc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48

def greeting[name: str] -> str:
    return 'Hello ' + name
72

Mới trong phiên bản 3. 10

đang gõ. AnyStr

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
196 là một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
197 được định nghĩa là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
198

Nó có nghĩa là được sử dụng cho các chức năng có thể chấp nhận bất kỳ loại chuỗi nào mà không cho phép các loại chuỗi khác nhau trộn lẫn. Ví dụ

def greeting[name: str] -> str:
    return 'Hello ' + name
73

lớp đang gõ. Giao thức[Chung]

Lớp cơ sở cho các lớp giao thức. Các lớp giao thức được định nghĩa như thế này

def greeting[name: str] -> str:
    return 'Hello ' + name
74

Các lớp như vậy chủ yếu được sử dụng với các trình kiểm tra kiểu tĩnh nhận biết kiểu con cấu trúc [gõ vịt tĩnh], chẳng hạn

def greeting[name: str] -> str:
    return 'Hello ' + name
75

Xem PEP 544 để biết thêm chi tiết. Các lớp giao thức được trang trí bằng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
199 [được mô tả sau] hoạt động như các giao thức thời gian chạy đơn giản, chỉ kiểm tra sự hiện diện của các thuộc tính đã cho, bỏ qua chữ ký loại của chúng

Các lớp giao thức có thể chung chung, ví dụ

def greeting[name: str] -> str:
    return 'Hello ' + name
76

Mới trong phiên bản 3. 8

@đang gõ. runtime_checkable

Đánh dấu một lớp giao thức là một giao thức thời gian chạy

Such a protocol can be used with

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
064 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
065. Điều này làm tăng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
061 khi áp dụng cho lớp không có giao thức. Điều này cho phép kiểm tra cấu trúc đơn giản, rất giống với “one trick pony” trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
803 chẳng hạn như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
651. Ví dụ

def greeting[name: str] -> str:
    return 'Hello ' + name
77

Note

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
199 sẽ chỉ kiểm tra sự hiện diện của các phương thức được yêu cầu, không phải chữ ký loại của chúng. Ví dụ:
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
806 là một lớp, do đó, nó vượt qua kiểm tra
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
065 đối với
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28. Tuy nhiên, phương thức
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
809 chỉ tồn tại để gọi một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
061 với thông báo nhiều thông tin hơn, do đó không thể gọi [khởi tạo]
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
806

Mới trong phiên bản 3. 8

Các chỉ thị đặc biệt khác¶

Chúng không được sử dụng trong chú thích. Họ đang xây dựng các khối để khai báo các loại

lớp đang gõ. NamedTuple

Phiên bản đánh máy của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
812

Usage

def greeting[name: str] -> str:
    return 'Hello ' + name
78

Điều này tương đương với

def greeting[name: str] -> str:
    return 'Hello ' + name
79

Để cung cấp cho một trường một giá trị mặc định, bạn có thể gán cho nó trong nội dung lớp

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
10

Các trường có giá trị mặc định phải xuất hiện sau bất kỳ trường nào không có giá trị mặc định

Lớp kết quả có một thuộc tính bổ sung

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
813 đưa ra một lệnh ánh xạ tên trường với các loại trường. [Tên trường nằm trong thuộc tính
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
814 và giá trị mặc định nằm trong thuộc tính
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
815, cả hai thuộc tính này đều là một phần của API
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
816. ]

Các lớp con của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
817 cũng có thể có các tài liệu và phương thức

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
11

Các lớp con của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
817 có thể chung chung

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
12

Sử dụng tương thích ngược

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
13

Đã thay đổi trong phiên bản 3. 6. Đã thêm hỗ trợ cho cú pháp chú thích biến PEP 526.

Đã thay đổi trong phiên bản 3. 6. 1. Đã thêm hỗ trợ cho các giá trị, phương thức và chuỗi tài liệu mặc định.

Đã thay đổi trong phiên bản 3. 8. Các thuộc tính

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
819 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
813 hiện là từ điển thông thường thay vì phiên bản của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
821.

Đã thay đổi trong phiên bản 3. 9. Đã xóa thuộc tính

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
819 để sử dụng thuộc tính
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
813 tiêu chuẩn hơn có cùng thông tin.

Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ cho các bộ có tên chung.

lớp đang gõ. Kiểu mới[tên , tp]

Một lớp trợ giúp để chỉ ra một loại riêng biệt cho trình kiểm tra đánh máy, hãy xem NewType . Khi chạy, nó trả về một đối tượng trả về đối số của nó khi được gọi. Cách sử dụng.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
14

Mới trong phiên bản 3. 5. 2

Đã thay đổi trong phiên bản 3. 10. ______062 bây giờ là một lớp chứ không phải là một hàm.

lớp đang gõ. TypedDict[dict]

Cấu trúc đặc biệt để thêm gợi ý loại vào từ điển. Khi chạy nó là một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
825 đơn giản

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 khai báo một loại từ điển yêu cầu tất cả các phiên bản của nó có một bộ khóa nhất định, trong đó mỗi khóa được liên kết với một giá trị của một loại nhất quán. This expectation is not checked at runtime but is only enforced by type checkers. Cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
15

Để cho phép sử dụng tính năng này với các phiên bản Python cũ hơn không hỗ trợ PEP 526,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 hỗ trợ thêm hai dạng cú pháp tương đương

  • Sử dụng một chữ

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    825 làm đối số thứ hai

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    16

  • Sử dụng đối số từ khóa

    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    17

Không dùng nữa kể từ phiên bản 3. 11, sẽ bị xóa trong phiên bản 3. 13. Cú pháp đối số từ khóa không được dùng trong 3. 11 và sẽ bị xóa trong 3. 13. Nó cũng có thể không được hỗ trợ bởi bộ kiểm tra kiểu tĩnh.

Cú pháp chức năng cũng nên được sử dụng khi bất kỳ khóa nào không có mã định danh hợp lệ, chẳng hạn như vì chúng là từ khóa hoặc chứa dấu gạch nối. Thí dụ.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
18

Theo mặc định, tất cả các khóa phải có trong một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41. Có thể đánh dấu các khóa riêng lẻ là không bắt buộc bằng cách sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
54

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
19

Điều này có nghĩa là một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
831
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 có thể bỏ qua khóa
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
833

Cũng có thể đánh dấu tất cả các khóa là không bắt buộc theo mặc định bằng cách chỉ định tổng số là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
834

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30

Điều này có nghĩa là một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
831
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 có thể bỏ qua bất kỳ phím nào. Trình kiểm tra loại chỉ được mong đợi để hỗ trợ một chữ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
834 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 làm giá trị của đối số
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
839.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 là mặc định và bắt buộc phải có tất cả các mục được xác định trong nội dung lớp

Các khóa riêng lẻ của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
841
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 có thể được đánh dấu theo yêu cầu bằng cách sử dụng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
53

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
31

Loại

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 có thể kế thừa từ một hoặc nhiều loại
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 khác bằng cách sử dụng cú pháp dựa trên lớp. Cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
32

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
846 có ba mục.
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
072,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
848 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
849. Nó tương đương với định nghĩa này

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33

Một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 không thể kế thừa từ một lớp không phải ____041, ngoại trừ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
30. Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
34

Một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 có thể là chung chung

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
35

Có thể xem xét nội quan một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 thông qua các lệnh chú thích [xem Các phương pháp hay nhất về chú thích để biết thêm thông tin về các phương pháp hay nhất về chú thích],
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
855,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
856 và .

__total__

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
858 đưa ra giá trị của đối số
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
839. Thí dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
36

__required_keys__

Mới trong phiên bản 3. 9

__tùy chọn_keys__

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
860 và
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
861 lần lượt trả về các đối tượng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
862 chứa các khóa bắt buộc và không bắt buộc

Các khóa được đánh dấu bằng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
53 sẽ luôn xuất hiện trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
856 và các khóa được đánh dấu bằng
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
54 sẽ luôn xuất hiện trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
857

Để tương thích ngược với Python 3. 10 trở xuống, cũng có thể sử dụng tính kế thừa để khai báo cả khóa bắt buộc và khóa không bắt buộc trong cùng một

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41. Điều này được thực hiện bằng cách khai báo một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 với một giá trị cho đối số
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
839 và sau đó kế thừa từ nó trong một
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 khác với một giá trị khác cho
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
839

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
37

Mới trong phiên bản 3. 9

Xem PEP 589 để biết thêm ví dụ và quy tắc chi tiết về việc sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41

Mới trong phiên bản 3. 8

Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ để đánh dấu các khóa riêng lẻ là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
53 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
54. Xem PEP 655.

Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ cho các

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 chung.

Bộ sưu tập bê tông chung¶

Tương ứng với các loại tích hợp¶

lớp đang gõ. Dict[dict, MutableMapping[KT, VT]]

Một phiên bản chung của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
825. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
877

Loại này có thể được sử dụng như sau

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
38

Không dùng nữa kể từ phiên bản 3. 9. ______4878 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

lớp đang gõ. Danh sách[danh sách, MutableSequence[T]]

Phiên bản chung của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
115. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
881 hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
651

Loại này có thể được sử dụng như sau

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
39

Không dùng nữa kể từ phiên bản 3. 9. ______4883 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

lớp đang gõ. Set[set, MutableSet[T]]

Một phiên bản chung của

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
885. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
886

Không dùng nữa kể từ phiên bản 3. 9. ______4885 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. FrozenSet[frozenset, AbstractSet[T_co]]

A generic version of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
889

Deprecated since version 3. 9.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
889 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

Note

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
686 is a special form

Corresponding to types in
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
893¶

class typing. DefaultDict[collections. defaultdict, MutableMapping[KT, VT]]

A generic version of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
894

Mới trong phiên bản 3. 5. 2

Deprecated since version 3. 9.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
894 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. OrderedDict[collections. OrderedDict, MutableMapping[KT, VT]]

A generic version of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
897

New in version 3. 7. 2

Deprecated since version 3. 9.

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
897 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. ChainMap[collections. ChainMap, MutableMapping[KT, VT]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
400

Mới trong phiên bản 3. 5. 4

New in version 3. 6. 1

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
400 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Counter[collections. Counter, Dict[T, int]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
403

Mới trong phiên bản 3. 5. 4

New in version 3. 6. 1

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
403 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Deque[deque, MutableSequence[T]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
406

Mới trong phiên bản 3. 5. 4

New in version 3. 6. 1

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
406 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

Other concrete types¶

class typing. IOclass typing. TextIOclass typing. BinaryIO

Generic type

def greeting[name: str] -> str:
    return 'Hello ' + name
409 and its subclasses
def greeting[name: str] -> str:
    return 'Hello ' + name
410 and
def greeting[name: str] -> str:
    return 'Hello ' + name
411 represent the types of I/O streams such as returned by
def greeting[name: str] -> str:
    return 'Hello ' + name
412

Deprecated since version 3. 8, will be removed in version 3. 13. The

def greeting[name: str] -> str:
    return 'Hello ' + name
413 namespace is deprecated and will be removed. These types should be directly imported from
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 instead.

class typing. Patternclass typing. Match

These type aliases correspond to the return types from

def greeting[name: str] -> str:
    return 'Hello ' + name
415 and
def greeting[name: str] -> str:
    return 'Hello ' + name
416. These types [and the corresponding functions] are generic in
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
196 and can be made specific by writing
def greeting[name: str] -> str:
    return 'Hello ' + name
418,
def greeting[name: str] -> str:
    return 'Hello ' + name
419,
def greeting[name: str] -> str:
    return 'Hello ' + name
420, or
def greeting[name: str] -> str:
    return 'Hello ' + name
421

Deprecated since version 3. 8, will be removed in version 3. 13. Không gian tên

def greeting[name: str] -> str:
    return 'Hello ' + name
422 không được dùng nữa và sẽ bị xóa. Thay vào đó, các loại này nên được nhập trực tiếp từ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25.

Không dùng nữa kể từ phiên bản 3. 9. Các lớp

def greeting[name: str] -> str:
    return 'Hello ' + name
424 và
def greeting[name: str] -> str:
    return 'Hello ' + name
425 từ
def greeting[name: str] -> str:
    return 'Hello ' + name
426 hiện hỗ trợ
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657. Xem PEP 585 và Loại bí danh chung .

lớp đang gõ. Text

def greeting[name: str] -> str:
    return 'Hello ' + name
428 là bí danh của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33. Nó được cung cấp để cung cấp đường dẫn tương thích chuyển tiếp cho mã Python 2. in Python 2,
def greeting[name: str] -> str:
    return 'Hello ' + name
428 is an alias for
def greeting[name: str] -> str:
    return 'Hello ' + name
431

Use

def greeting[name: str] -> str:
    return 'Hello ' + name
428 to indicate that a value must contain a unicode string in a manner that is compatible with both Python 2 and Python 3

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
00

Mới trong phiên bản 3. 5. 2

Deprecated since version 3. 11. Python 2 is no longer supported, and most type checkers also no longer support type checking Python 2 code. Removal of the alias is not currently planned, but users are encouraged to use

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33 instead of
def greeting[name: str] -> str:
    return 'Hello ' + name
428 wherever possible.

Abstract Base Classes¶

Corresponding to collections in
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
803¶

class typing. AbstractSet[Collection[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
436

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
436 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. ByteString[Sequence[int]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
439

This type represents the types

def greeting[name: str] -> str:
    return 'Hello ' + name
440,
def greeting[name: str] -> str:
    return 'Hello ' + name
441, and
def greeting[name: str] -> str:
    return 'Hello ' + name
442 of byte sequences

As a shorthand for this type,

def greeting[name: str] -> str:
    return 'Hello ' + name
440 can be used to annotate arguments of any of the types mentioned above

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
439 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Collection[Sized, Iterable[T_co], Container[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
446

New in version 3. 6. 0

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
446 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Container[Generic[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
449

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
449 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. ItemsView[MappingView, AbstractSet[tuple[KT_co, VT_co]]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
452

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
452 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. KeysView[MappingView, AbstractSet[KT_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
455

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
455 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Mapping[Collection[KT], Generic[KT, VT_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
458. This type can be used as follows

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
01

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
458 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. MappingView[Sized]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
461

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
461 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. MutableMapping[Mapping[KT, VT]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
464

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
464 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. MutableSequence[Sequence[T]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
467

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
467 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

lớp đang gõ. MutableSet[AbstractSet[T]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
470

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
470 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Sequence[Reversible[T_co], Collection[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
473

Deprecated since version 3. 9. ______5473 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. ValuesView[MappingView, Collection[_VT_co]]

Một phiên bản chung của

def greeting[name: str] -> str:
    return 'Hello ' + name
476

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
476 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

Corresponding to other types in
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
803¶

class typing. Iterable[Generic[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
480

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
480 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Iterator[Iterable[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
483

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
483 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Generator[Iterator[T_co], Generic[T_co, T_contra, V_co]]

A generator can be annotated by the generic type

def greeting[name: str] -> str:
    return 'Hello ' + name
486. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
02

Note that unlike many other generics in the typing module, the

def greeting[name: str] -> str:
    return 'Hello ' + name
487 of
def greeting[name: str] -> str:
    return 'Hello ' + name
488 behaves contravariantly, not covariantly or invariantly

If your generator will only yield values, set the

def greeting[name: str] -> str:
    return 'Hello ' + name
487 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
007 to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
03

Alternatively, annotate your generator as having a return type of either

def greeting[name: str] -> str:
    return 'Hello ' + name
492 or
def greeting[name: str] -> str:
    return 'Hello ' + name
493

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
04

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
494 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Hashable

An alias to

def greeting[name: str] -> str:
    return 'Hello ' + name
496

class typing. Reversible[Iterable[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
497

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
497 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. Xem PEP 585 và Loại bí danh chung .

class typing. Sized

An alias to

def greeting[name: str] -> str:
    return 'Hello ' + name
700

Asynchronous programming¶

class typing. Coroutine[Awaitable[V_co], Generic[T_co, T_contra, V_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
701. The variance and order of type variables correspond to those of
def greeting[name: str] -> str:
    return 'Hello ' + name
488, for example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
05

Mới trong phiên bản 3. 5. 3

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
701 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. AsyncGenerator[AsyncIterator[T_co], Generic[T_co, T_contra]]

An async generator can be annotated by the generic type

def greeting[name: str] -> str:
    return 'Hello ' + name
705. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
06

Unlike normal generators, async generators cannot return a value, so there is no

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
007 type parameter. As with
def greeting[name: str] -> str:
    return 'Hello ' + name
488, the
def greeting[name: str] -> str:
    return 'Hello ' + name
487 behaves contravariantly

If your generator will only yield values, set the

def greeting[name: str] -> str:
    return 'Hello ' + name
487 to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
07

Alternatively, annotate your generator as having a return type of either

def greeting[name: str] -> str:
    return 'Hello ' + name
711 or
def greeting[name: str] -> str:
    return 'Hello ' + name
712

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
08

New in version 3. 6. 1

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
713 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. AsyncIterable[Generic[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
715

Mới trong phiên bản 3. 5. 2

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
715 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. AsyncIterator[AsyncIterable[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
718

Mới trong phiên bản 3. 5. 2

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
718 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. Awaitable[Generic[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
721

Mới trong phiên bản 3. 5. 2

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
721 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

Các loại trình quản lý ngữ cảnh¶

class typing. Trình quản lý bối cảnh[Chung[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
724

Mới trong phiên bản 3. 5. 4

New in version 3. 6. 0

Deprecated since version 3. 9.

def greeting[name: str] -> str:
    return 'Hello ' + name
724 now supports subscripting [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

class typing. AsyncContextManager[Generic[T_co]]

A generic version of

def greeting[name: str] -> str:
    return 'Hello ' + name
727

Mới trong phiên bản 3. 5. 4

Mới trong phiên bản 3. 6. 2

Không dùng nữa kể từ phiên bản 3. 9. ______5727 hiện hỗ trợ đăng ký [

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
657]. See PEP 585 and Generic Alias Type .

Protocols¶

These protocols are decorated with

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
199

class typing. SupportsAbs

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
731 that is covariant in its return type

class typing. SupportsBytes

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
732

class typing. SupportsComplex

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
733

class typing. SupportsFloat

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
734

class typing. SupportsIndex

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
735

Mới trong phiên bản 3. 8

class typing. SupportsInt

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
736

lớp đang gõ. SupportsRound

An ABC with one abstract method

def greeting[name: str] -> str:
    return 'Hello ' + name
737 that is covariant in its return type

Functions and decorators¶

typing. cast[typ , val]

Cast a value to a type

This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don’t check anything [we want this to be as fast as possible]

typing. assert_type[val , typ , /]

Ask a static type checker to confirm that val has an inferred type of typ

When the type checker encounters a call to

def greeting[name: str] -> str:
    return 'Hello ' + name
738, it emits an error if the value is not of the specified type

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
09

At runtime this returns the first argument unchanged with no side effects

This function is useful for ensuring the type checker’s understanding of a script is in line with the developer’s intentions

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
10

Mới trong phiên bản 3. 11

typing. assert_never[arg , /]

Ask a static type checker to confirm that a line of code is unreachable

Thí dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
11

Here, the annotations allow the type checker to infer that the last case can never execute, because

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
108 is either an
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
63 or a
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
33, and both options are covered by earlier cases. If a type checker finds that a call to
def greeting[name: str] -> str:
    return 'Hello ' + name
742 is reachable, it will emit an error. For example, if the type annotation for
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
108 was instead
def greeting[name: str] -> str:
    return 'Hello ' + name
744, the type checker would emit an error pointing out that
def greeting[name: str] -> str:
    return 'Hello ' + name
745 is of type
def greeting[name: str] -> str:
    return 'Hello ' + name
746. For a call to
def greeting[name: str] -> str:
    return 'Hello ' + name
747 to pass type checking, the inferred type of the argument passed in must be the bottom type,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
670, and nothing else

At runtime, this throws an exception when called

See also

Unreachable Code and Exhaustiveness Checking has more information about exhaustiveness checking with static typing

Mới trong phiên bản 3. 11

typing. reveal_type[obj , /]

Reveal the inferred static type of an expression

When a static type checker encounters a call to this function, it emits a diagnostic with the type of the argument. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
12

This can be useful when you want to debug how your type checker handles a particular piece of code

The function returns its argument unchanged, which allows using it within an expression

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
13

Most type checkers support

def greeting[name: str] -> str:
    return 'Hello ' + name
749 anywhere, even if the name is not imported from
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25. Importing the name from
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 allows your code to run without runtime errors and communicates intent more clearly

At runtime, this function prints the runtime type of its argument to stderr and returns it unchanged

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
14

Mới trong phiên bản 3. 11

@typing. dataclass_transform

def greeting[name: str] -> str:
    return 'Hello ' + name
752 may be used to decorate a class, metaclass, or a function that is itself a decorator. The presence of
def greeting[name: str] -> str:
    return 'Hello ' + name
753 tells a static type checker that the decorated object performs runtime “magic” that transforms a class, giving it
def greeting[name: str] -> str:
    return 'Hello ' + name
754-like behaviors

Example usage with a decorator function

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
15

On a base class

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
16

On a metaclass

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
17

The

def greeting[name: str] -> str:
    return 'Hello ' + name
755 classes defined above will be treated by type checkers similarly to classes created with
def greeting[name: str] -> str:
    return 'Hello ' + name
756. For example, type checkers will assume these classes have
def greeting[name: str] -> str:
    return 'Hello ' + name
757 methods that accept
def greeting[name: str] -> str:
    return 'Hello ' + name
758 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
32

The decorated class, metaclass, or function may accept the following bool arguments which type checkers will assume have the same effect as they would have on the

def greeting[name: str] -> str:
    return 'Hello ' + name
756 decorator.
def greeting[name: str] -> str:
    return 'Hello ' + name
761,
def greeting[name: str] -> str:
    return 'Hello ' + name
762,
def greeting[name: str] -> str:
    return 'Hello ' + name
763,
def greeting[name: str] -> str:
    return 'Hello ' + name
764,
def greeting[name: str] -> str:
    return 'Hello ' + name
765,
def greeting[name: str] -> str:
    return 'Hello ' + name
766,
def greeting[name: str] -> str:
    return 'Hello ' + name
767, and
def greeting[name: str] -> str:
    return 'Hello ' + name
768. It must be possible for the value of these arguments [
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 or
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
834] to be statically evaluated

The arguments to the

def greeting[name: str] -> str:
    return 'Hello ' + name
752 decorator can be used to customize the default behaviors of the decorated class, metaclass, or function

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    772 indicates whether the
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    762 parameter is assumed to be
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    099 or
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    834 if it is omitted by the caller

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    776 indicates whether the
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    763 parameter is assumed to be True or False if it is omitted by the caller

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    778 indicates whether the
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    767 parameter is assumed to be True or False if it is omitted by the caller

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    780 specifies a static list of supported classes or functions that describe fields, similar to
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    781

  • Các đối số từ khóa khác tùy ý được chấp nhận để cho phép các tiện ích mở rộng có thể có trong tương lai

Type checkers recognize the following optional arguments on field specifiers

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    761 indicates whether the field should be included in the synthesized
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    757 method. If unspecified,
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    761 defaults to
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    099

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    786 provides the default value for the field

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    787 provides a runtime callback that returns the default value for the field. If neither
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    786 nor
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    787 are specified, the field is assumed to have no default value and must be provided a value when the class is instantiated

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    790 is an alias for
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    787

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    767 cho biết liệu trường có nên được đánh dấu là chỉ từ khóa hay không. If
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    099, the field will be keyword-only. If
    Vector = list[float]
    
    def scale[scalar: float, vector: Vector] -> Vector:
        return [scalar * num for num in vector]
    
    # passes type checking; a list of floats qualifies as a Vector.
    new_vector = scale[2.0, [1.0, -4.2, 5.4]]
    
    834, it will not be keyword-only. If unspecified, the value of the
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    767 parameter on the object decorated with
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    752 will be used, or if that is unspecified, the value of
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    778 on
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    752 will be used

  • def greeting[name: str] -> str:
        return 'Hello ' + name
    
    799 provides an alternative name for the field. This alternative name is used in the synthesized
    def greeting[name: str] -> str:
        return 'Hello ' + name
    
    757 method

At runtime, this decorator records its arguments in the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
101 attribute on the decorated object. It has no other runtime effect

See PEP 681 for more details

Mới trong phiên bản 3. 11

@typing. quá tải

The

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102 decorator allows describing functions and methods that support multiple different combinations of argument types. A series of
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated definitions must be followed by exactly one non-
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated definition [for the same function/method]. The
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated definitions are for the benefit of the type checker only, since they will be overwritten by the non-
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated definition, while the latter is used at runtime but should be ignored by a type checker. At runtime, calling a
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated function directly will raise
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
108. An example of overload that gives a more precise type than can be expressed using a union or a type variable

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
18

See PEP 484 for more details and comparison with other typing semantics

Changed in version 3. 11. Overloaded functions can now be introspected at runtime using

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
109.

typing. get_overloads[func]

Return a sequence of

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102-decorated definitions for func. func is the function object for the implementation of the overloaded function. For example, given the definition of
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
111 in the documentation for
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
102,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
113 will return a sequence of three function objects for the three defined overloads. If called on a function with no overloads,
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
109 returns an empty sequence

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
109 can be used for introspecting an overloaded function at runtime

Mới trong phiên bản 3. 11

typing. clear_overloads[]

Clear all registered overloads in the internal registry. This can be used to reclaim the memory used by the registry

Mới trong phiên bản 3. 11

@đang gõ. final

A decorator to indicate to type checkers that the decorated method cannot be overridden, and the decorated class cannot be subclassed. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
19

Không có kiểm tra thời gian chạy của các thuộc tính này. Xem PEP 591 để biết thêm chi tiết

Mới trong phiên bản 3. 8

Changed in version 3. 11. The decorator will now set the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
116 attribute to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 on the decorated object. Thus, a check like
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
118 can be used at runtime to determine whether an object
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
119 has been marked as final. If the decorated object does not support setting attributes, the decorator returns the object unchanged without raising an exception.

@đang gõ. no_type_check

Trình trang trí để chỉ ra rằng các chú thích không phải là gợi ý loại

This works as class or function decorator . With a class, it applies recursively to all methods and classes defined in that class [but not to methods defined in its superclasses or subclasses].

This mutates the function[s] in place

@typing. no_type_check_decorator

Decorator to give another decorator the

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
120 effect

This wraps the decorator with something that wraps the decorated function in

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
120

@typing. type_check_only

Decorator to mark a class or function to be unavailable at runtime

This decorator is itself not available at runtime. It is mainly intended to mark classes that are defined in type stub files if an implementation returns an instance of a private class

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
20

Note that returning instances of private classes is not recommended. It is usually preferable to make such classes public

Introspection helpers¶

typing. get_type_hints[obj , globalns=None , localns=None , include_extras=False]

Return a dictionary containing type hints for a function, method, module or class object

This is often the same as

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
122. In addition, forward references encoded as string literals are handled by evaluating them in
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
123 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
124 namespaces. For a class
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
041, return a dictionary constructed by merging all the
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
813 along
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
127 in reverse order

The function recursively replaces all

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
128 with
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
603, unless
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
130 is set to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 [see
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 for more information]. For example

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
21

Note

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
088 does not work with imported type aliases that include forward references. Enabling postponed evaluation of annotations [PEP 563] may remove the need for most forward references.

Changed in version 3. 9. Added

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
130 parameter as part of PEP 593.

Changed in version 3. 11. Previously,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
135 was added for function and method annotations if a default value equal to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60 was set. Now the annotation is returned unchanged.

typing. get_args[tp]typing. get_origin[tp]

Provide basic introspection for generic types and special typing forms

For a typing object of the form

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
137 these functions return
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
138 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
139. If
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
138 is a generic alias for a builtin or
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
893 class, it gets normalized to the original class. Nếu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
138 là một liên kết hoặc
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
40 được chứa trong một loại chung khác, thứ tự của
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
139 có thể khác với thứ tự của các đối số ban đầu
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
145 do bộ nhớ đệm loại. For unsupported objects return
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
60 and
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
147 correspondingly. Examples

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
22

Mới trong phiên bản 3. 8

typing. is_typeddict[tp]

Check if a type is a

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41

Ví dụ

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
23

Mới trong phiên bản 3. 10

class typing. ForwardRef

A class used for internal typing representation of string forward references. For example,

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
149 is implicitly transformed into
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
150. This class should not be instantiated by a user, but may be used by introspection tools

Note

PEP 585 generic types such as

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
151 will not be implicitly transformed into
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
152 and thus will not automatically resolve to
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
153

New in version 3. 7. 4

Không thay đổi¶

đang gõ. TYPE_CHECKING

Một hằng số đặc biệt được giả định là

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
099 bởi trình kiểm tra loại tĩnh của bên thứ 3. Đó là
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
834 trong thời gian chạy. Cách sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
24

Chú thích loại đầu tiên phải được đặt trong dấu ngoặc kép, làm cho nó trở thành "tham chiếu chuyển tiếp", để ẩn tham chiếu

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
156 khỏi thời gian chạy trình thông dịch. Loại chú thích cho các biến cục bộ không được đánh giá, vì vậy chú thích thứ hai không cần phải được đặt trong dấu ngoặc kép

Note

Nếu sử dụng

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
157, các chú thích sẽ không được đánh giá tại thời điểm định nghĩa hàm. Thay vào đó, chúng được lưu trữ dưới dạng chuỗi trong
Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
813. Điều này làm cho việc sử dụng dấu ngoặc kép xung quanh chú thích là không cần thiết [xem PEP 563]

Mới trong phiên bản 3. 5. 2

Deprecation Timeline of Major Features¶

Certain features in

Vector = list[float]

def scale[scalar: float, vector: Vector] -> Vector:
    return [scalar * num for num in vector]

# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
25 are deprecated and may be removed in a future version of Python. The following table summarizes major deprecations for your convenience. This is subject to change, and not all deprecations are listed

What is the type of lambda Python?

Python Lambda Functions are anonymous function means that the function is without a name. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python.

Are type hints Pythonic?

No, type hints don't force Python to check the type of data being stored in a variable . They are just for the developer's reference and help auto-completion software. They allow IDEs to recognise data types and give suggestions based on this information.

Does lambda contain return statements?

Hàm lambda không cần câu lệnh trả về , chúng luôn trả về một biểu thức duy nhất.

Chủ Đề