Thời gian chạy Python không thực thi chức năng và chú thích loại biến. Chúng có thể được sử dụng bởi các công cụ của bên thứ ba như trình kiểm tra loại, IDE, linters, v.v.
Mô-đun này cung cấp hỗ trợ thời gian chạy cho các gợi ý loại. Hỗ trợ cơ bản nhất bao gồm các loại , , , và. Để biết thông số kỹ thuật đầy đủ, vui lòng xem PEP 484. Để có phần giới thiệu đơn giản về gợi ý nhập, hãy xem PEP 483
Hàm bên dưới nhận và trả về một chuỗi và được chú thích như sau
def greeting[name: str] -> str: return 'Hello ' + name
Trong 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]]31, đố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]]32 được mong đợi là kiểu và kiểu trả về. Các kiểu con được chấp nhận làm đối số
Các tính năng mới thường xuyên được thêm vào 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. Gói typing_extensions cung cấp các bản sao của các tính năng mới này cho các phiên bản Python cũ hơn
Để biết tóm tắt về các tính năng không dùng nữa và dòng thời gian không dùng nữa, vui lòng xem
Xem thêm
Tài liệu tại https. //đánh máy. đọcthedocs. io/ đóng vai trò là tài liệu tham khảo hữu ích cho các tính năng của hệ thống nhập, các công cụ liên quan đến đánh máy hữu ích và các phương pháp hay nhất về đánh máy
PEP liên quan
Kể từ lần đầu giới thiệu các gợi ý loại trong PEP 484 và PEP 483, một số PEP đã sửa đổi và nâng cao khung của Python cho các chú thích loại. Bao gồm các
- PEP 526. Cú pháp cho chú thích biến
Giới thiệu cú pháp để chú thích các biến bên ngoài định nghĩa hàm và
- PEP 544. giao thức. Phân loại cấu trúc [gõ vịt tĩnh]
Giới thiệu và trang trí
- PEP 585. Nhập gợi ý chung trong bộ sưu tập tiêu chuẩn
Giới thiệu và khả năng sử dụng các lớp thư viện tiêu chuẩn như
- PEP 586. Các loại chữ
giới thiệu
- 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
- PEP 591. Thêm một vòng loại cuối cùng để gõ
Giới thiệu và trang trí
- PEP 593. Chức năng linh hoạt và chú thích biến
giới thiệu
- 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]]
45Giới thiệu và khả năng sử dụng toán tử nhị phân 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]]
47 để biểu thị một - PEP 612. Tham số Thông số kỹ thuật Biến
Giới thiệu và
- PEP 613. Bí danh loại rõ ràng
giới thiệu
- PEP 646. Generic biến thể
giới thiệu
- PEP 647. Bảo vệ loại do người dùng xác định
giới thiệu
- PEP 655. Đánh dấu các mục TypedDict riêng lẻ là bắt buộc hoặc có khả năng bị thiếu
Giới thiệu và
- PEP 673. tự loại
giới thiệu
- PEP 675. Loại chuỗi ký tự tùy ý
giới thiệu
- PEP 681. Biến đổi lớp dữ liệu
Giới thiệu người trang trí
Nhập bí danh
Bí danh loại được xác định bằng cách gán loại cho bí danh. Trong ví dụ 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]]58 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]]59 sẽ được coi là từ đồng nghĩa có thể hoán đổi cho nhau
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
Bí danh loại rất hữu ích để đơn giản hóa chữ ký loại phức tạp. Ví dụ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...
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]]60 dưới dạng gợi ý loại là trường hợp đặc biệt và được thay thế 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]]61
Loại mới
Sử dụng trình trợ giúp để tạo các loại riêng 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]]2
Trình kiểm tra kiểu tĩnh sẽ xử lý kiểu mới như thể nó là một lớp con của kiểu ban đầu. Điều này rất hữu ích trong việc giúp bắt các lỗi logic
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Bạn vẫn có thể thực hiện tất cả các hoạt độ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]]63 trên một biến 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]]64, nhưng kết quả sẽ luôn là 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]]63. Điều này cho phép bạn vượt qua 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]]64 bất cứ nơi nào có thể mong đợ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]]63, nhưng sẽ ngăn bạn vô tình 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]]64 theo cách 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]]0
Lưu ý rằng các kiểm tra này chỉ được thực thi bởi trình kiểm tra kiểu tĩnh. Trong thời gian chạy, câu lệ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]]69 sẽ biế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]]70 thành một hàm có thể gọi được và ngay lập tức trả về bất kỳ tham số nào bạn truyền cho nó. Điều đó có nghĩa là biểu 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]]71 không tạo ra một lớp mới hoặc giới thiệu nhiều chi phí ngoài cuộc gọi hàm thông thường
Chính xác hơn, biểu 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]]72 luôn đúng trong thời gian chạy
Không hợp lệ để tạo một loại phụ 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]]70
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Tuy nhiên, có thể tạo dựa trên 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]]62 'có nguồ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]]8
và kiểm tra đánh máy 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]]76 sẽ hoạt động như mong đợi
Xem PEP 484 để biết thêm chi tiết
Ghi chú
Nhớ lại rằng việc sử dụng bí danh kiểu khai báo hai kiểu tương đương với nhau. Thực hiệ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]]77 sẽ khiến trình kiểm tra kiểu tĩnh coi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 hoàn toà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]]79 trong mọi trường hợp. Điều này hữu ích khi bạn muốn đơn giản hóa chữ ký loại phức tạp
Ngược 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]]62 tuyên bố một kiểu là kiểu con của kiểu khác. Việc thực hiệ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]]81 sẽ khiến trình kiểm tra kiểu tĩnh coi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 là một phân 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]]79, có nghĩa là không thể sử dụng 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]]79 ở những nơi mong đợi 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]]70. Đ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. ______162 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 ý loạ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]]2
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 cuộc gọi lấy các cuộc 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. 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, thì toán tử 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. ______128 hiện đang hỗ trợ và. Xem PEP 612 để biết thêm chi tiết.
Xem thêm
Tài liệu về và 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 gốc
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]]8
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 gọ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]]0
Loại chung do người dùng xác định
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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...01 với tư cách là một lớp cơ sở định nghĩa rằng lớp
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...02 nhận một tham số kiểu duy nhất là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03. Điều này cũng làm cho
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03 hợp lệ như một kiểu trong thân lớp
Lớp cơ sở định nghĩa sao cho
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...07 có giá trị như một 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]]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 đượ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 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 nhiều kế thừa 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]]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,
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...11 có một tham số duy nhất,
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03
Sử dụng một lớp chung mà không chỉ định tham số loại giả định cho từng vị trí. Trong ví dụ sau,
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...14 không phải là chung chung nhưng hoàn toàn kế thừa từ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...15
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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. không còn siêu dữ liệu tùy chỉnh nữa.
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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...17. 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]]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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...19 và cả
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...20 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...0
Xin lưu ý rằng thuốc generic với có thể không có
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...22 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. hiện có thể được tham số hóa qua các biểu thức tham số. Xem 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
Một loại đặc biệt của loại là. Trình kiểm tra loại tĩnh sẽ coi mọi loại là tương thích với và 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 và gán nó cho bất kỳ biến nào
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...1
Lưu ý rằng không có kiểm tra loại nào được thực hiện khi gán giá trị của loại 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...31 cho
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...32 mặc dù
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...32 được khai báo là kiểu và nhận giá trị 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 sử dụng
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...2
Hành vi này cho phép được sử dụng như một lối thoát hiểm khi bạn cần kết hợp mã được nhập động và tĩnh
Đối chiếu hành vi của với hành vi của. Tương tự như , mỗi loại là một kiểu con của. Tuy nhiên, không giống như , điều ngược lại là không đúng. không phải là một 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à , trình kiểm tra loại sẽ từ chối hầu hết mọi 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 một loại chuyên biệt hơn là lỗi loại. Ví dụ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...3
Sử dụng để chỉ ra rằng một giá trị có thể là bất kỳ loại nào theo cách an toàn. Sử dụng để chỉ ra rằng một giá trị được nhập động
Phân nhóm danh nghĩa và 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à một lớp
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...47 được cho phép khi một lớp
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...48 được mong đợi nếu và chỉ khi
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...47 là một lớp con của
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...48
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ư. 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...4
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ó các lớp cơ sở rõ ràng trong định nghĩa lớp, cho phép
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...52 được coi là một kiểu con của cả
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...53 và
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...54 bởi các bộ 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]
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...5
Ngoài ra, bằng cách phân lớp một lớp đặc biệt, 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 nhóm cấu trúc [xem ví dụ bên dưới]
nội dung mô-đun
Mô-đun định nghĩa các lớp, chức năng và trình trang trí sau
Ghi chú
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 có sẵn, cũng mở rộng để hỗ trợ các biến loại bên trong
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57. 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ợ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57
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
Các loại không dùng nữa sẽ bị xóa khỏi mô-đun trong phiên bản Python đầu tiên được phát hành 5 năm sau khi phát hành 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ợ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57đ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
tương thích với mọi loại
Đã thay đổi trong phiên bản 3. 11. 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ụ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...6
Đ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ề
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...7
Mới trong phiên bản 3. 11. Trên các phiên bản Python cũ hơn, có thể được sử dụng để diễn đạt cùng một khái niệm.
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...70 đã được thêm vào để làm cho ý nghĩa rõ ràng hơn. đang gõ. Không trả lại
Loại đặc biệt chỉ ra rằng một hàm không bao giờ trả về. Ví dụ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...8
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...69 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, loại nên được sử dụng cho khái niệm này để thay thế. 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ânLoại đặc biệt để đại diện cho lớp kèm theo hiện tại. Ví dụ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...9
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]]20
Nói chung nếu một cái gì đó hiện đang theo mô hì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]]21
Bạn nên sử dụng as gọi tới
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...74 sẽ có kiểu trả về là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...75 chứ không phải là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...76
Các trường hợp sử dụng phổ biến khác bao gồm
s được sử dụng làm hàm tạo thay thế và trả về các phiên bản của tham số
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...
78Chú thích một phương thức trả về self
Xem PEP 673 để biết thêm chi tiết
Mới trong phiên bản 3. 11
đang gõ. TypeAliasChú thích đặc biệt để khai báo rõ ràng một. 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]]22
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
các hình thức đặ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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57, mỗi loại có một cú pháp duy nhấtđang gõ. Bộ
Loại tuple; . Loại của bộ dữ liệu trống có thể được viết là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...82
Thí dụ.
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...83 là một bộ gồm hai phần tử tương ứng với các biến kiểu T1 và T2.
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...84 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.
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...85. Một đồng bằng tương đương với
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...87, và đến lượt nó
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. đ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.
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...93 hoặc viết tắt là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...94. Sử dụng tốc ký đó được khuyến khích. 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]]
23Liê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]]
24Đố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]]
25Khi 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]]
26Bạ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]]
27Bạn không thể viết
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...
96
Đã 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. Thấy.
loại tùy chọn
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...98 tương đương với
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...99 [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]]200]
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 từ 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]]201 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]]27
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]]201 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]]28
Đã thay đổi trong phiên bản 3. 10. Tùy chọn bây giờ có thể được viết là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...99. Thấy. đang gõ. Có thể gọi
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]]207. Một đồng bằng 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]]209, và đến lượt nó
Các cuộc gọi lấy các cuộc 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. 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, thì toán tử 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. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
Đã thay đổi trong phiên bản 3. 10. ______128 hiện đang hỗ trợ và. Xem PEP 612 để biết thêm chi tiết.
Xem thêm
Tài liệu về và 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 và để nhập chú thích 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]]225.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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. 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à dấu chấm lửng 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]]230]
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]]231 cung cấp a cho hàm đượ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]]231 mong đợi một hàm có thể gọi được, nhận vào 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]]235 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, chỉ ra rằng các loại tham số có thể gọi được trả về phụ thuộc vào các loại tham số của 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]]29
Mới trong phiên bản 3. 10
Xem thêm
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]và
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]]241 có thể chấp nhận 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]]241. 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]]243 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]]241. 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]]30
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]]243 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]]31
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]]243 là hiệp phương sai 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]]241 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]]241. 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 là các lớp, , 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]]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]]251 tương đương với ________ 1249, đến lượt nó tương đương với ________ 1253, 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. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. đang gõ. Chữ
Một loại có thể được sử dụng để chỉ ra 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]]33
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]256 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]]256, 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. ______140 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 giờ đây sẽ đưa ra một ngoại lệ trong quá trình so sánh đẳng thức nếu một trong các tham số của chúng không. đ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]]34
chỉ chấp nhận các loại và không thể đăng ký thêm
bản thân nó không phải là một lớp và không nên được sử dụng với hoặc. 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]]35
Mới trong phiên bản 3. 5. 3
đang gõ. Cuối cùngMộ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]]36
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ộcCác cấu trúc gõ đặc biệt đánh dấu các phím riêng lẻ của a là bắt buộc hoặc không bắt buộc tương ứng
Xem và PEP 655 để biết thêm chi tiết
Mới trong phiên bản 3. 11
đang gõ. Chú thíchMột loại, được giới thiệu trong PEP 593 [_______1269], để 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03 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]]272 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]]273. 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]]273 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]]272, thì thư viện đó nên bỏ qua nó và chỉ coi loại đó là
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03. 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]]277 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03 [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]]272 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]]272 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. , 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]]264]
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]]37
Vượt 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]]287 để 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 loại chú thích đượ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]]
38Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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]]
292 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]]
39Cá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ùngVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
00Các 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]]
01Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 chungVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Mới trong phiên bản 3. 9
đang gõ. TypeGuardBiể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 chỉ chấp nhận một đối số loại duy nhất. 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]]03
Đô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 hàm 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]]297 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]]298 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
Giá trị trả về là một boolean
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]]
299, loại đối số của nó là loại bên trongVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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]]04
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]]301 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
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...78 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]]304
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]]305, 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]]306 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]]299, 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]]308 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]]309 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]]310
Ghi 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]]310 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]]309 – 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]]313 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]]314 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]]315 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õ. ChungLớ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]]05
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]]06lớp đang gõ. TypeVar
Loại biến
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]]07
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 để 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]]08
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]]09
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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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 vấ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]]29 khi chính xác là một trong những ràng buộ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]]61
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]]320 sẽ tăng. Nói chung, và không nên được sử dụng với các loại
Các 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]]324 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]]325. Xem PEP 484 để biết thêm chi tiết. Theo mặc định, các biến loại là bất biếnlớp đang gõ. TypeVarTuple
Loại tuple biến. Một hình thức chuyên biệt cho phép tạo ra các thuốc generic khác nhau
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 một số kiểu tùy ý bằng cách hành động giống như một số tùy ý các biến kiểu được bọc trong một bộ. 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]]62
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]]327 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]]328. 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]]329 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]]330.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]328 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]]332, 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]]333. [Lưu ý rằng trong các phiên bản cũ hơn của Python, thay vào đó, bạn có thể thấy điều này được viết bằng cách sử dụ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]]335. ]
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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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ề
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Cuối cùng, một tuple 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]]336
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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]]336 - 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]]338, 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]]340 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]]336. Ở đâ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]]336 đượ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]]343 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]]344
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énToá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]]327 trên a 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]]334 để đánh dấu bộ biến kiểu là đã được 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]]68
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]]334 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]]327 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]]334 đượ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]]327 ở một số nơi nhấ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]]69
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. Phiên bản chuyên dụng của
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]]80
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 để biết thêm thông tin về các loại chung
Ví dụ: để thêm chức năng ghi nhật ký cơ bản, 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]]356 để ghi nhật ký các lệnh 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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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]]209 bị ràng buộc. Tuy nhiên điều này gây ra hai vấn đề
Trình kiểm tra loại không thể gõ kiểm tra 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]]
360 vì phải 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]]
336 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]]
362có 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]]
356 khi trả về hàmVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
360 hoặc trình kiểm tra kiểu tĩnh phải được yêu cầu bỏ quaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
367
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 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]]369 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]]370 để 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]]369 đạ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]]336.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]370 đại diện cho ánh xạ các tham số từ khóa tớ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]]362. 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]]369 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]]370 lần lượt là các thể hiện của và
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]]324 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]]325 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]]382 cũng được chấp nhận, tương tự như. 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
Ghi chú
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
Xem thêm
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]và
Các đối số và các thuộc tính đối số từ khóa của một. 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]]369 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]]378, 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]]370 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]]379. 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 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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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. 10
đang gõ. AnyStrVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]396 đượ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]]398
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ụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]83lớ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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Xem PEP 544 để biết thêm chi tiết. Các lớp giao thức được trang trí [đượ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 nhất định, bỏ qua các chữ ký loại của chúng
Các lớp giao thức có thể chung chung, 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]]86
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
Một giao thức như vậy có thể được sử dụng với và. Điều này tăng lên khi áp dụng cho một lớp phi 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” chẳng hạn như. 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]]87
Ghi chú
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ụ, là một lớp, do đó nó vượt qua kiểm tra đối với. 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]]009 chỉ tồn tại để đưa ra một thông báo có nhiều thông tin hơn, do đó không thể gọi [khởi tạo]
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õ. NamedTuplePhiên bản đánh máy của
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
Điều này 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]]89
Để 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]]20
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]]013 đư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]]014 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]]015, cả hai đều là một phần của API. ]
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]]017 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]]21
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]]017 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]]22
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]]23
Đã 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]]019 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]]013 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]]021.
Đã 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]]019 để 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]]013 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 khác biệt cho trình đánh máy, xem. 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]]24
Mới trong phiên bản 3. 5. 2
Đã thay đổi trong phiên bản 3. 10. ______162 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. Trong thời gian chạy nó là một đồng 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]]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. Kỳ vọng này không được kiểm tra trong thời gian chạy mà chỉ được thực thi bởi trình kiểm tra loạ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]]25
Để 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 nghĩa đen 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]]
26Sử 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]]
27
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 hợp lệ, chẳng hạn 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]]28
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]]29
Đ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]]031
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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]]033
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]]034
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Đ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]]031
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 dự kiến sẽ 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]]034 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]]299 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]]039.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]299 là giá trị mặc định và yêu cầu 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]]041
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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]]81
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]]82
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]046 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]]272,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]048 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]]049. 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]]83
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 ____141, ngoại 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]]84
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]]85
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ể được xem xét nội tâm thông qua các lệnh chú thích [xem để biết thêm thông tin về các phương pháp hay nhất về chú thích], , , và__toàn bộ__
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]058 đư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]]039. 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]]86__required_keys__
Mới trong phiên bản 3. 9
__tùy chọn_phí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]]060 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]]061 lần lượt trả về các đối tượng 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 sẽ luôn xuất hiện trong ________ 2056 và các khóa được đánh dấu bằng sẽ luôn xuất hiện trong ________ 2057
Để 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]]039 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]]039
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
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à hoặc. 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. 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ư
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]]88
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Danh sách[danh sách, MutableSequence[T]]
Phiên bản chung của. 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 một loại tập hợp trừu tượng như hoặc
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]]89
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Set[set, MutableSet[T]]
Một phiên bản chung của. 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ư
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. FrozenSet[frozenset, AbstractSet[T_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
Ghi chú
là một hình thức đặc biệt
Tương ứng với các loại trong
lớp đang gõ. DefaultDict[bộ sưu tập. defaultdict, MutableMapping[KT, VT]]Một phiên bản chung của
Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. OrderedDict[bộ sưu tập. OrderedDict, MutableMapping[KT, VT]]
Một phiên bản chung của
Mới trong phiên bản 3. 7. 2
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Bản đồ chuỗi[bộ sưu tập. ChainMap, MutableMapping[KT, VT]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Bộ đếm[bộ sưu tập. Bộ đếm, Dict[T, int]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Deque[deque, MutableSequence[T]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
Các loại bê tông khác
lớp đang gõ. IOlớp đang gõ. TextIOlớp đang gõ. Nhị phânIOLoạ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]]609 và các lớp con 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]]610 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]]611 đại diện cho các loại luồng I/O chẳng hạn như được trả về bởi
Không dùng nữa kể từ phiên bản 3. 8, sẽ bị xóa trong phiên bản 3. 13. Không gian tê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]]613 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. lớp đang gõ. Mẫulớp gõ. Trận đấu
Các bí danh loại này tương ứng với các loại trả về từ và. Các loại này [và các chức năng tương ứng] là chung chung 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]]396 và có thể được làm cụ thể bằng cách 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]]618,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]621
Không dùng nữa kể từ phiên bản 3. 8, sẽ bị xóa trong phiên bản 3. 13. Không gian tê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]]622 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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]624 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]]625 từ giờ hỗ trợ
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57. Xem PEP 585 và. lớp đang gõ. Văn bả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]]628 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. trong Python 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]]628 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]]631
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]]628 để cho biết rằng một giá trị phải chứa chuỗi unicode theo cách tương thích với cả Python 2 và 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
Không dùng nữa kể từ phiên bản 3. 11. Python 2 không còn được hỗ trợ và hầu hết các trình kiểm tra kiểu cũng không còn hỗ trợ kiểm tra kiểu mã Python 2. Việc xóa bí danh hiện chưa được lên kế hoạch, nhưng người dùng được khuyến khích sử dụng 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]]628 bất cứ khi nào có thể.
Các lớp cơ sở trừu tượng
Tương ứng với các bộ sưu tập trong
lớp đang gõ. Bộ trừu tượng[Bộ sưu tập[T_co]]Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. ByteString[Sequence[int]]
Một phiên bản chung của
Loại này đại diện cho các loại , và của các chuỗi byte
Là cách viết tắt của loại này, có thể được sử dụng để chú thích đối số của bất kỳ loại nào được đề cập ở trên
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Bộ sưu tập[Có kích thước, Có thể lặp lại[T_co], Container[T_co]]
Một phiên bản chung của
Mới trong phiên bản 3. 6. 0
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Vùng chứa[Chung[T_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. ItemsView[MappingView, AbstractSet[tuple[KT_co, VT_co]]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. KeysView[MappingView, AbstractSet[KT_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Ánh xạ[Bộ sưu tập[KT], Chung[KT, VT_co]]
Một phiên bản chung của. 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]]01
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Chế độ xem bản đồ[Đã định cỡ]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Mapping có thể thay đổi[Mapping[KT, VT]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Sequence có thể thay đổi[Sequence[T]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. MutableSet[AbstractSet[T]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Trình tự[Có thể đảo ngược[T_co], Bộ sưu tập[T_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Chế độ xem giá trị[Chế độ xem bản đồ, Bộ sưu tập[_VT_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
Tương ứng với các loại khác trong
lớp đang gõ. Có thể lặp lại[Chung[T_co]]Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Iterator[Iterable[T_co]]
Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Trình tạo[Iterator[T_co], Chung[T_co, T_contra, V_co]]
Trình tạo có thể được chú thích theo loại 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]]686. 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
Lưu ý rằng không giống như nhiều thuốc generic khác trong mô-đun đá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]]687 của hoạt động trái ngược, không đồng biến hoặc bất biến
Nếu trình tạo của bạn chỉ mang lại giá trị, hãy đặ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]]687 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]]207 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]]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
Ngoài ra, hãy chú thích trình tạo của bạn 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]]692 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]]693
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Có thể băm
Một bí danh cho
lớp đang gõ. Có thể đảo ngược[Có thể lặp lại[T_co]]Một phiên bản chung của
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Có kích thước
Một bí danh cho
lập trình không đồng bộ
lớp đang gõ. Coroutine[Awaitable[V_co], Chung[T_co, T_contra, V_co]]Một phiên bản chung của. Phương sai và thứ tự của các biến loại tương ứng với biến của , 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]]05
Mới trong phiên bản 3. 5. 3
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. AsyncGenerator[AsyncIterator[T_co], Generic[T_co, T_contra]]
Trình tạo không đồng bộ có thể được chú thích theo 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]]805. 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]]06
Không giống như các trình tạo thông thường, trình tạo không đồng bộ không thể trả về giá trị, do đó không có tham số 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]]207. Như 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 hoạt động trái ngược
Nếu trình tạo của bạn chỉ mang lại giá trị, hãy đặ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]]687 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]]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
Ngoài ra, hãy chú thích trình tạo của bạn 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]]811 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]]812
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. AsyncIterable[Chung[T_co]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. AsyncIterator[AsyncIterable[T_co]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. Có thể chờ đợi[Chung[T_co]]
Một phiên bản chung của
Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
Các loại trình quản lý bối cảnh
lớp đang gõ. Trình quản lý bối cảnh[Chung[T_co]]Một phiên bản chung của
Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 0
Không dùng nữa kể từ phiên bản 3. 9. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và. lớp đang gõ. AsyncContextManager[Chung[T_co]]
Một phiên bản chung của
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. hiện hỗ trợ đăng ký [
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...57]. Xem PEP 585 và.
giao thức
Các giao thức này được trang trí với
lớp đang gõ. Hỗ trợ AbsMột ABC với một phương thức trừu 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]]831 đồng biến trong kiểu trả về của nólớp đang gõ. Số byte hỗ trợ
Một ABC với một phương pháp trừu 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]]832lớp đang gõ. Hỗ trợ Phức hợp
Một ABC với một phương pháp trừu 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]]833lớp đang gõ. Hỗ trợFloat
Một ABC với một phương pháp trừu 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]]834lớp đang gõ. Chỉ số hỗ trợ
Một ABC với một phương pháp trừu 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]]835
Mới trong phiên bản 3. 8
lớp đang gõ. Hỗ trợIntMột ABC với một phương pháp trừu 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]]836lớp đang gõ. Vòng hỗ trợ
Một ABC với một phương thức trừu 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]]837 đồng biến trong kiểu trả về của nó
Chức năng và trang trí
đang gõ. truyền[typ , val]Truyền một giá trị cho một loại
Điều này trả về giá trị không thay đổi. Đối với trình kiểm tra loại, điều này báo hiệu rằng giá trị trả về có loại được chỉ định, nhưng trong thời gian chạy, chúng tôi cố tình không kiểm tra bất kỳ thứ gì [chúng tôi muốn điều này càng nhanh càng tốt]
đang gõ. assert_type[val , typ, /]Yêu cầu trình kiểm tra loại tĩnh xác nhận rằng val có loại được suy luận
Khi trình kiểm tra loại gặp 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]]838, nó sẽ báo lỗi nếu giá trị không thuộc loại đã chỉ đị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]]09
Khi chạy, điều này trả về đối số đầu tiên không thay đổi mà không có tác dụng phụ
Chức năng này hữu ích để đảm bảo sự hiểu biết của trình kiểm tra loại về tập lệnh phù hợp với ý định của nhà phát triể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]]10
Mới trong phiên bản 3. 11
đang gõ. assert_never[arg , /]Yêu cầu trình kiểm tra loại tĩnh xác nhận rằng một dòng mã không thể truy cập được
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
Ở đây, các chú thích cho phép trình kiểm tra loại suy luận rằng trường hợp cuối cùng không bao giờ có thể thực thi, bởi 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]]308 là an hoặc a và cả hai tùy chọn đều nằm trong trường hợp trước đó. Nếu trình kiểm tra loại thấy rằng có thể truy cập 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]]842, nó sẽ phát ra lỗi. Ví dụ: nếu chú thích 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]]308 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]]844, trình kiểm tra loại sẽ phát ra lỗi 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]]845 thuộc loại. Đối với một cuộc gọi đế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]]847 để vượt qua kiểm tra loại, loại suy luận của đối số được truyền vào phải là loại dưới cùng, và không có gì khác
Khi chạy, điều này ném một ngoại lệ khi được gọi
Xem thêm
Kiểm tra mức độ đầy đủ và mã không thể truy cập có thêm thông tin về kiểm tra mức độ đầy đủ bằng cách gõ tĩnh
Mới trong phiên bản 3. 11
đang gõ. reveal_type[obj , /]Tiết lộ loại tĩnh được suy luận của một biểu thức
Khi trình kiểm tra kiểu tĩnh gặp lệnh gọi hàm này, nó sẽ phát ra chẩn đoán với kiểu đối số. 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
Điều này có thể hữu ích khi bạn muốn gỡ lỗi cách trình kiểm tra loại của bạn xử lý một đoạn mã cụ thể
Hàm trả về đối số của nó không thay đổi, cho phép sử dụng nó trong một biểu 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]]13
Hầu hết các trình kiểm tra loại đều 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]]849 ở mọi nơi, ngay cả khi tên không được nhậ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. Nhập tê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]]25 cho phép mã của bạn chạy mà không có lỗi thời gian chạy và truyền đạt ý định rõ ràng hơn
Trong thời gian chạy, hàm này in kiểu thời gian chạy của đối số của nó thành thiết bị lỗi chuẩn và trả về nó không thay đổ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]]14
Mới trong phiên bản 3. 11
@đang gõ. dataclass_transformcó thể được sử dụng để trang trí một lớp, siêu dữ liệu hoặc một chức năng mà chính nó là một công cụ trang trí. Sự hiện diệ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]]853 cho một trình kiểm tra kiểu tĩnh biết rằng đối tượng được trang trí thực hiện “phép thuật” trong thời gian chạy để biến đổi một lớp, mang lại cho nó các hành vi giống như
Ví dụ sử dụng với chức năng 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]]15
Trên một 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]]16
Trên một 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]]17
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]]855 được xác định ở trên sẽ được xử lý bằng trình kiểm tra loại tương tự như các lớp được tạo bằng. Ví dụ: bộ kiểm tra loại sẽ cho rằng các lớp này có các 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]]857 chấp nhậ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]]858 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]]32
Lớp, siêu dữ liệu hoặc hàm được trang trí có thể chấp nhận các đối số bool sau đây mà trình kiểm tra loại sẽ cho rằng có tác dụng tương tự như đối với 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]]861,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]863,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]864,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]865,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]866,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]867 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]]868. Giá trị của các đối số 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]]299 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]]034] phải có thể được đánh giá tĩnh
Các đối số cho 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]]852 có thể được sử dụng để tùy chỉnh các hành vi mặc định của lớp, siêu dữ liệu hoặc hàm được 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]]
872 cho biết 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]]
862 được coi 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]]
299 hayVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
034 nếu nó bị người gọi bỏ quaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
876 cho biết liệu 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]]
863 được coi là Đúng hay Sai nếu nó bị người gọi bỏ quaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
878 cho biết liệu 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]]
867 được coi là Đúng hay Sai nếu nó bị người gọi bỏ quaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
880 chỉ định một danh sách tĩnh các lớp hoặc hàm được hỗ trợ mô tả các trường, 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]]
881Cá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
Trình kiểm tra loại nhận ra các đối số tùy chọn sau trên bộ xác định trườ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]]
861 cho biết liệu trường có nên được đưa vào phương phápVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 hợp hay không. Nếu không được chỉ đị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]]
861 mặc đị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]]
299Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 cung cấp giá trị mặc định cho trườngVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
887 cung cấp gọi lại thời gian chạy trả về giá trị mặc định cho trường. Nếu 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]]
886 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]]
887 đều không được chỉ định, thì trường được coi là không có giá trị mặc định và phải được cung cấp một giá trị khi lớp được khởi tạoVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
890 là bí danh củaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
887Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
867 cho biết liệu trường có nên được đánh dấu là chỉ từ khóa hay không. NếuVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
299, trường sẽ chỉ có từ khóa. NếuVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
034, nó sẽ không chỉ có từ khóa. Nếu không được chỉ định, giá trị 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]]
867 trên đối tượng được trang trí bằngVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
852 sẽ được sử dụng hoặc nếu không được chỉ định, giá trị củaVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
878 trênVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
852 sẽ được sử dụngVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
899 cung cấp tên thay thế cho trường. Tên thay thế này được sử dụng trong phương phápVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 hợp
Khi chạy, trình trang trí này ghi lại các đối số của nó 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]]201 trên đối tượng được trang trí. Nó không có hiệu ứng thời gian chạy khác
Xem PEP 681 để biết thêm chi tiết
Mới trong phiên bản 3. 11
@đang gõ. quá tảiTrì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]]202 cho phép mô tả các hàm và phương thức hỗ trợ nhiều kiểu kết hợp đối số khác nhau. Một loạt các định nghĩa được 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]]202 phải được theo sau bởi chính xác một định nghĩa không được trang trí ____4202 [cho cùng một chức năng/phương thức]. Các định nghĩa đượ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]]202 chỉ dành cho lợi ích của trình kiểm tra loại, vì chúng sẽ bị ghi đè bởi định nghĩa không được trang trí ____4202, trong khi định nghĩa sau được sử dụng trong thời gian chạy nhưng trình kiểm tra loại nên bỏ qua. Khi chạy, việc gọi trực tiếp một hàm đượ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]]202 sẽ tăng. Một ví dụ về quá tải cung cấp một loại chính xác hơn có thể được biểu thị bằng cách sử dụng liên kết hoặc biến 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]]18
Xem PEP 484 để biết thêm chi tiết và so sánh với các ngữ nghĩa đánh máy khác
Đã thay đổi trong phiên bản 3. 11. Các hàm bị quá tải giờ đây có thể được xem xét nội tại khi chạy bằng cách sử dụng.
đang gõ. get_overloads[func]Trả về một chuỗi các định nghĩa -decorated cho func. func là đối tượng chức năng để thực hiện chức năng quá tải. Ví dụ: đưa ra định nghĩa 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]]211 trong 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]]213 sẽ trả về một chuỗi gồm ba đối tượng hàm cho ba lần quá tải đã xác định. Nếu được gọi trên một hàm không có quá 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]]209 sẽ trả về một chuỗi trố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]]209 có thể được sử dụng để xem xét nội hàm một hàm bị quá tải khi chạy
Mới trong phiên bản 3. 11
đang gõ. clear_overloads[]Xóa tất cả các quá tải đã đăng ký trong sổ đăng ký nội bộ. Điều này có thể được sử dụng để lấy lại bộ nhớ được sử dụng bởi sổ đăng ký
Mới trong phiên bản 3. 11
@đang gõ. cuối cùngMột trình trang trí để chỉ ra cho người kiểm tra loại rằng phương thức được trang trí không thể bị ghi đè và lớp được trang trí không thể được phân lớp. 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]]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
Đã thay đổi trong phiên bản 3. 11. Người trang trí bây giờ sẽ đặt 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]]216 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]]299 trên đối tượng được trang trí. Do đó, một kiểm tra 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]]218 có thể được sử dụng trong thời gian chạy để xác định xem một đố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]]219 đã được đánh dấu là cuối cùng hay chưa. Nếu đối tượng được trang trí không hỗ trợ cài đặt thuộc tính, trình trang trí sẽ trả về đối tượng không thay đổi mà không đưa ra ngoại lệ. @đ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
Điều này hoạt động như lớp hoặc chức năng. Với một lớp, nó áp dụng đệ quy cho tất cả các phương thức và lớp được định nghĩa trong lớp đó [nhưng không áp dụng cho các phương thức được định nghĩa trong lớp cha hoặc lớp con của nó]
Điều này làm thay đổi [các] chức năng tại chỗ
@đang gõ. no_type_check_decoratorTrang trí để cung cấp cho một trang trí khác hiệu ứng
Điều này bao bọc trình trang trí bằng thứ gì đó bao bọc chức năng được trang trí trong
@đang gõ. type_check_onlyTrình trang trí để đánh dấu một lớp hoặc chức năng không khả dụng khi chạy
Bản thân trình trang trí này không có sẵn trong thời gian chạy. Nó chủ yếu nhằm đánh dấu các lớp được định nghĩa trong các tệp sơ khai kiểu nếu việc triển khai trả về một thể hiện của một lớp riê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]]20
Lưu ý rằng không nên trả lại các phiên bản của các lớp riêng tư. Tốt nhất là công khai các lớp như vậy
người trợ giúp nội tâm
đang gõ. get_type_hints[obj , globalns=None, localns=None, include_extras=False]Trả về một từ điển chứa các gợi ý kiểu cho một hàm, phương thức, mô-đun hoặc đối tượng lớp
Điều này thườ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]]222. Ngoài ra, các tham chiếu chuyển tiếp được mã hóa dưới dạng chuỗi ký tự được xử lý bằng cách đánh giá chúng trong các không gian tê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]]223 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]]224. Đối với một 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]]241, hãy trả về một từ điển được tạo bằng cách hợp nhất tất cả 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]]013 dọc theo
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]227 theo thứ tự ngược lại
Hàm thay thế đệ quy tất 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]]228 bằng
from collections.abc import Sequence ConnectionOptions = dict[str, str] Address = tuple[str, int] Server = tuple[Address, ConnectionOptions] def broadcast_message[message: str, servers: Sequence[Server]] -> None: ... # The static type checker will treat the previous type signature as # being exactly equivalent to this one. def broadcast_message[ message: str, servers: Sequence[tuple[tuple[str, int], dict[str, str]]]] -> None: ...03, trừ khi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]230 được đặt 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]]299 [xem để biết thêm thông tin]. 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]]21
Ghi chú
không hoạt động với nhập bao gồm các tham chiếu chuyển tiếp. Cho phép đánh giá chú thích bị trì hoãn [PEP 563] có thể loại bỏ nhu cầu đối với hầu hết các tham chiếu chuyển tiếp
Đã thay đổi trong phiên bản 3. 9. Đã thêm 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]]230 như một phần của PEP 593.
Đã thay đổi trong phiên bản 3. 10. 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]]288 trên một lớp không còn trả về các chú thích của các lớp cơ sở của nó.
Đã thay đổi trong phiên bản 3. 11. Trước đâ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]]236 đã được thêm vào cho các chú thích hàm và phương thức nếu giá trị mặc định 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]]60 được đặt. Bây giờ chú thích được trả lại không thay đổi. đang gõ. get_args[tp]đang gõ. get_origin[tp]
Cung cấp nội quan cơ bản cho các loại chung và các hình thức gõ đặc biệt
Đối với một đối tượng gõ 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]]238, các hàm này 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]]239 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]]240. 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]]239 là bí danh chung cho nội trang hoặc lớp, nó sẽ được chuẩn hóa thành lớp ban đầu. 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]]239 là một liên kết hoặc đượ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]]240 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]]246 do bộ nhớ đệm loại. Đối với các đối tượng không được hỗ trợ, hãy 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]]60 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]]248 tương ứ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]]22
Mới trong phiên bản 3. 8
đang gõ. được đánh máy[đến]Kiểm tra xem một loại có phải là một
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
lớp đang gõ. ForwardRefMột lớp được sử dụng để biểu diễn kiểu gõ bên trong của các tham chiếu chuyển tiếp chuỗ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]]250 được chuyển đổi ngầm 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]]251. Lớp này không nên được khởi tạo bởi người dùng, nhưng có thể được sử dụng bởi các công cụ hướng nội
Ghi chú
Các loại chung PEP 585 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]]252 sẽ không được chuyển đổi hoàn toàn 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]]253 và do đó sẽ không tự động phân giải 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]]254
Mới trong phiên bản 3. 7. 4
Hằng số
đang gõ. TYPE_CHECKINGMộ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]]299 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]]034 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]]257 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
Ghi chú
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]]258, 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]]013. Đ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
Dòng thời gian ngừng sử dụng các tính năng chính
Một số tính nă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]]25 không được dùng nữa và có thể bị xóa trong phiên bản tương lai của Python. Bảng sau đây tóm tắt các loại bỏ chính để thuận tiện cho bạn. Điều này có thể thay đổi và không phải tất cả các phản đối đều được liệt kê