Trong khi chạy mã Python3 của tôi, tôi đang gặp lỗi dưới đây:
'TypeError:' Type 'đối tượng không thể đăng ký'
Hệ thống không thể nhận ra mô -đun gõ có tham số danh sách trong khi tôi đang cố gắng đặt đối số
20 10 GFG27 là
20 10 GFG28
Tôi đã cài đặt mô -đun
20 10 GFG29 qua
20 10 GFG30 và cố gắng
20 10 GFG31
Ngay cả sau đó hệ thống không nhận ra loại
20 10 GFG28 trong định nghĩa lớp.
Dưới đây là mã cho cùng,
from typing import List
class newcls:
def __init__[self, a: int,arr: list[int]]:
self.a=a
self.arr=arr
def new1[self]:
print['a=',self.a]
print[self.arr]
obj1=newcls[1,[1,2,3]]
obj1.new1[]
Và thông báo lỗi tôi nhận được khi chạy,
Lỗi thời gian chạy như def init [self, a: int, mảng: list [int]]:init[self, a: int,arr: list[int]]:
TypeError: Đối tượng 'Loại' không thể đăng ký
20
10
GFG
98 Phiên bản của các bộ sưu tập tiêu chuẩn
Chưa quyết định
PEP 585How to import a class from another file in Python.
__ Mô -đun __ Python là gì?
Thuộc tính __module__ được dự định để truy xuất mô-đun trong đó hàm được xác định, để đọc mã nguồn hoặc đôi khi để nhập lại nó trong một tập lệnh.
Lệnh nhập khẩu trong Python là gì?
Trong Python, bạn sử dụng từ khóa nhập để làm mã trong một mô -đun có sẵn trong một mô -đun khác. Nhập khẩu trong Python rất quan trọng để cấu trúc mã của bạn một cách hiệu quả. Sử dụng nhập khẩu đúng cách sẽ giúp bạn làm việc hiệu quả hơn, cho phép bạn sử dụng lại mã trong khi giữ cho các dự án của bạn có thể duy trì.
Cải thiện bài viết
Lưu bài viết
Trong bài viết này, chúng ta sẽ xem cách nhập một lớp từ một tệp khác trong Python.
Nhập vào Python tương tự như #include header_file trong c/c ++. Các mô -đun Python có thể nhận được quyền truy cập vào mã từ một mô -đun khác bằng cách nhập tệp/chức năng bằng Nhập. Tuyên bố nhập khẩu là cách phổ biến nhất để gọi máy móc nhập khẩu, nhưng nó không phải là cách duy nhất. Câu lệnh nhập bao gồm từ khóa nhập cùng với tên của mô -đun.
Bắt đầu
Ở đây chúng tôi đã tạo một lớp có tên GFG có hai phương thức: thêm [] và sub []. Ngoài ra, một hàm rõ ràng được tạo ra có tên phương thức [] trong cùng một tệp python. Tệp này sẽ hoạt động như một mô -đun cho tệp Python chính.
20 10 GFG35
20 10 GFG58
20 10 GFG59
20 10 GFG60
20 10 GFG61
Python module.py.
20
10
GFG
33 20
10
GFG
34
20 10 GFG35
20 10 GFG36
20 10 GFG37
20 10 GFG38
20 10 GFG39module and create the object of the class named GFG inside that module. Now, we can use its methods and variables.
Trong Python, bạn sử dụng từ khóa nhập để làm mã trong một mô -đun có sẵn trong một mô -đun khác. Nhập khẩu trong Python rất quan trọng để cấu trúc mã của bạn một cách hiệu quả. Sử dụng nhập khẩu đúng cách sẽ giúp bạn làm việc hiệu quả hơn, cho phép bạn sử dụng lại mã trong khi giữ cho các dự án của bạn có thể duy trì.
Cải thiện bài viết
Lưu bài viết
20 10 GFG58
20 10 GFG59
20 10 GFG64
20 10 GFG70
20 10 GFG71
20 10 GFG72
20 10 GFG73
20 10 GFG74
20 10 GFG58
20 10 GFG59
20 10 GFG64
20 10 GFG78
20 10 GFG71
20 10 GFG72
20 10 GFG73
20 10 GFG74
20 10 GFG83
Output:
20 10 GFG
Trong bài viết này, chúng ta sẽ xem cách nhập một lớp từ một tệp khác trong Python.
Python
20 10 GFG84
20 10 GFG85
20 10 GFG62
20 10 GFG87
20 10 GFG88
Output:
GFG
Theo cách này, chúng ta có thể sử dụng lớp để nhập từ một tệp khác.
Trong Python, bạn sử dụng từ khóa nhập để làm mã trong một mô -đun có sẵn trong một mô -đun khác. Nhập khẩu trong Python rất quan trọng để cấu trúc mã của bạn một cách hiệu quả. Sử dụng nhập khẩu đúng cách sẽ giúp bạn làm việc hiệu quả hơn, cho phép bạn sử dụng lại mã trong khi giữ cho các dự án của bạn có thể duy trì.
Mới trong phiên bản 3.5. Lib/typing.py
Mã nguồn: lib/gõ.py
Ghi chú
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, linter, v.v.PEP 484. For a simplified introduction to type hints, see PEP 483.
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
20 10 GFG89,
20 10 GFG90,
20 10 GFG91,
20 10 GFG92 và
20 10 GFG93. Để 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ề các gợi ý loại, xem PEP 483.
def greeting[name: str] -> str: return 'Hello ' + name
Hàm bên dưới lấy và trả về một chuỗi và được chú thích như sau:
Trong hàm
20 10 GFG94, đối số
20 10 GFG95 dự kiến sẽ thuộc loại
20 10 GFG96 và loại trả về
20 10 GFG96. Các kiểu con được chấp nhận là đối số.
Các tính năng mới thường được thêm vào mô -đun
20 10 GFG98. 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 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 dòng thời gian không dùng nữa của các tính năng chính.
Xem thêm
Tài liệu tại //typing.readthedocs.io/ làm tài liệu tham khảo hữu ích cho các tính năng hệ thống loại, gõ các công cụ liên quan hữu ích và gõ các thực tiễn tốt nhất.
Peps¶ có liên quanPEP 484 and PEP 483, a number of PEPs have modified and enhanced Python’s framework for type annotations. These include:
- Kể từ khi giới thiệu ban đầu các gợi ý loại trong PEP 484 và PEP 483, một số PEP đã sửa đổi và tăng cường khung Python tựa cho các chú thích loại. Bao gồm các:: Syntax for Variable
Annotations
PEP 526: Cú pháp để 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à
20 10 GFG
99: Protocols: Structural subtyping [static duck typing]PEP 544: Các giao thức: Substing cấu trúc [gõ vịt tĩnh]
- Giới thiệu
GFG
00 và nhà trang tríGFG
01: Type Hinting Generics In Standard CollectionsPEP 585: Loại Generics Generics trong các bộ sưu tập tiêu chuẩngeneric types
- Giới thiệu
GFG
02 và khả năng sử dụng các lớp thư viện tiêu chuẩn làm loại chung chung: Literal TypesPEP 586: Các loại nghĩa đen
- Giới thiệu
GFG
03: TypedDict: Type Hints for Dictionaries with a Fixed Set of KeysPEP 589: TypedDict: Loại gợi ý cho từ điển với một bộ khóa cố định
- Giới thiệu
GFG
04: Adding a final qualifier to typingPEP 591: Thêm một vòng loại cuối cùng để đánh máy
- Giới thiệu
GFG
05 và người trang tríGFG
06: Flexible function and variable annotationsPEP 593: Chức năng linh hoạt và Chú thích thay đổi
- Giới thiệu
GFG
07: Allow writing union types asGFG
08PEP 604: Cho phép viết các loại liên minh là
GFG
08union of types - Giới thiệu
GFG
09 và khả năng sử dụng toán tử nhị phânGFG
10 để biểu thị sự kết hợp của các loại: Parameter Specification VariablesPEP 612: Biến đặc tả tham số
- Giới thiệu
GFG
11 vàGFG
12: Explicit Type AliasesPEP 613: Bí danh loại rõ ràng
- Giới thiệu
GFG
13: Variadic GenericsPEP 646: Generics Variadic
- Giới thiệu
GFG
14: User-Defined Type GuardsPEP 647: Người bảo vệ loại do người dùng xác định
- Giới thiệu
GFG
15: Marking individual TypedDict items as required or potentially missingPEP 655: Đánh dấu các mục cá nhân đánh máy theo yêu cầu hoặc có khả năng bị thiếu
- Giới thiệu
GFG
16 vàGFG
17: Self typePEP 673: Loại tự
- Giới thiệu
GFG
18: Arbitrary Literal String TypePEP 675: Loại chuỗi theo nghĩa đen tùy ý
- Giới thiệu
GFG
19: Data Class TransformsPEP 681: Biến đổi lớp dữ liệu
Giới thiệu người trang trí GFG
20
Gõ bí danh
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
Một bí danh loại được xác định bằng cách gán loại cho bí danh. Trong ví dụ này,
GFG21 và
GFG22 sẽ được coi là từ đồng nghĩa có thể hoán đổi cho nhau:
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: ...
Bí danh loại rất hữu ích để đơn giản hóa chữ ký loại phức. Ví dụ:
Lưu ý rằng GFG
23 dưới dạng gợi ý loại là một trường hợp đặc biệt và được thay thế bằng GFG
24.
Kiểu mới¶
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]
Sử dụng Trình trợ giúp
GFG25 để tạo các loại khác nhau:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
Trình kiểm tra loại tĩnh sẽ xử lý loại mới như thể nó là một lớp con của loại ban đầu. Điều này rất hữu ích trong việc giúp bắt các lỗi logic:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]
Bạn vẫn có thể thực hiện tất cả các hoạt động
GFG26 trên một biến của loại
GFG27, nhưng kết quả sẽ luôn luôn thuộc loại
GFG26. Điều này cho phép bạn vượt qua trong một
GFG27 bất cứ nơi nào có thể mong đợi
GFG26, nhưng sẽ ngăn bạn vô tình tạo ra
GFG27 theo cách không hợp lệ:
Chính xác hơn, biểu thức
GFG35 luôn đúng khi chạy.
Nó không hợp lệ khi tạo một loại phụ của
GFG33:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass
Tuy nhiên, có thể tạo ra một
GFG25 dựa trên một ____ ____2225:
20 10 GFG0
và đánh máy cho
GFG39 sẽ hoạt động như mong đợi.
Xem PEP 484 để biết thêm chi tiết.PEP 484 for more details.
Ghi chú
Hãy nhớ lại rằng việc sử dụng một bí danh loại tuyên bố hai loại tương đương với nhau. Thực hiện
GFG40 sẽ làm cho trình kiểm tra loại tĩnh điều trị
GFG41 là chính xác tương đương với
GFG42 trong mọi trường hợp. Điều này rất 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,
GFG25 tuyên bố một loại là một loại phụ của loại khác. Thực hiện
GFG44 sẽ làm cho trình kiểm tra loại tĩnh xử lý
GFG33 dưới dạng lớp con của
GFG42, có nghĩa là giá trị của loại
GFG42 không thể được sử dụng ở những nơi dự kiến giá trị của loại
GFG33. Điều này rất hữu ích khi bạn muốn ngăn chặn 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: GFG
25 hiện 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 GFG
25 qua chức năng thông thường. Tuy nhiên, chi phí này sẽ được giảm trong 3.11.0.
GFG25 is now a class rather than a function. There is some additional runtime cost when calling
GFG25 over a regular function. However, this cost will be reduced in 3.11.0.
Gọi là có thể gọi được
Các khung mong đợi các chức năng gọi lại của chữ ký cụ thể có thể được loại gợi ý bằng cách sử dụng
GFG51.
Ví dụ:
20 10 GFG1
Có thể khai báo loại trả về của một cuộc gọi có thể gọi mà không chỉ định chữ ký cuộc gọi bằng cách thay thế một dấu chấm lửng theo nghĩa đen cho danh sách các đối số trong loại gợi ý:
GFG52.
Các thiết bị gọi lấy các thiết bị 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
GFG11. Ngoài ra, nếu có thể gọi được thêm hoặc xóa các đối số khỏi các loại gọi khác, toán tử
GFG12 có thể được sử dụng. Họ lần lượt nhận dạng
GFG55 và
GFG56.
Thuốc generics;
Vì thông tin loại về các đối tượng được giữ trong các container không thể được suy ra tĩnh một cách 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 yếu tố container.
20 10 GFG2
Generics có thể được tham số hóa bằng cách sử dụng một nhà máy có sẵn trong việc gõ gọi là
20 10 GFG92.
20 10 GFG3
Các loại chung do người dùng xác định
Một lớp do người dùng xác định có thể được định nghĩa là một lớp chung.
20 10 GFG4
GFG58 Là một lớp cơ sở định nghĩa rằng lớp
GFG59 lấy một tham số loại duy nhất
GFG60. Điều này cũng làm cho
GFG60 hợp lệ dưới dạng một loại trong cơ thể lớp.
Lớp cơ sở
20 10 GFG93 xác định
GFG63 để
GFG64 hợp lệ dưới dạng loại:
20 10 GFG5
Một loại chung có thể có bất kỳ số lượng biến loại. Tất cả các loại của
20 10 GFG92 đều được cho phép làm tham số cho một loại chung:
20 10 GFG6
Mỗi đối số biến loại thành
20 10 GFG93 phải khác biệt. Điều này không hợp lệ:
20 10 GFG7
Bạn có thể sử dụng nhiều kế thừa với
20 10 GFG93:
20 10 GFG8
Khi kế thừa từ các lớp chung, một số biến loại có thể được sửa:
20 10 GFG9
Trong trường hợp này
GFG68 có một tham số duy nhất,
GFG60.
Sử dụng một lớp chung mà không chỉ định các tham số loại giả định
20 10 GFG89 cho mỗi vị trí. Trong ví dụ sau,
GFG71 không chung chung mà là kế thừa ngầm từ
GFG72:
GFG0
Bí danh loại chung được xác định của người dùng cũng được hỗ trợ. Ví dụ:
GFG1
Đã thay đổi trong phiên bản 3.7: 20
10
GFG
93 không còn có Metaclass tùy chỉnh.
20 10 GFG93 no longer has a custom metaclass.
Generics do người dùng xác định 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
GFG74. Hành vi phù hợp với các biến loại, được mô tả ở trên là các biến đặc tả tham số được coi bởi mô -đun gõ là một biến loại chuyên dụng. 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
GFG11:
GFG2
Hơn nữa, một chung chỉ có một biến đặc tả tham số sẽ chấp nhận danh sách tham số trong các biểu mẫu
GFG76 và cả
GFG77 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:
GFG3
Xin lưu ý rằng các chất generic với
GFG11 có thể không đúng
GFG79 sau khi thay thế trong một số trường hợp vì chúng được dự định chủ yếu để kiểm tra loại tĩnh.
Đã thay đổi trong phiên bản 3.10: 20
10
GFG
93 hiện có thể được tham số hóa qua các biểu thức tham số. Xem GFG
11 và PEP 612 để biết thêm chi tiết.
20 10 GFG93 can now be parameterized over parameter expressions. See
GFG11 and PEP 612 for more details.
Một lớp chung do người dùng xác định có thể có ABC như các lớp cơ sở mà không có xung đột metaclass. Các metaclass chung không được hỗ trợ. Kết quả của việc chung hóa tham số hóa được lưu trữ và hầu hết các loại trong mô -đun gõ đều có thể băm và có thể so sánh cho sự bình đẳng.
Loại 20
10
GFG
89
Một loại đặc biệt là
20 10 GFG89. Một trình kiểm tra loại tĩnh sẽ coi mọi loại là tương thích với
20 10 GFG89 và
20 10 GFG89 là tương thích với mọi loại.
Điều này có nghĩa là có thể thực hiện bất kỳ hoạt động hoặc phương thức gọi trên giá trị của loại
20 10 GFG89 và gán nó cho bất kỳ biến nào:
GFG4
Lưu ý rằng không có kiểm tra loại nào được thực hiện khi gán giá trị loại
20 10 GFG89 cho loại chính xác hơn. Ví dụ: Trình kiểm tra loại tĩnh đã không báo cáo lỗi khi gán
GFG88 cho
GFG89 mặc dù
GFG89 đã được tuyên bố là loại
20 10 GFG96 và nhận được giá trị
GFG26 khi chạy!
Hơn nữa, tất cả các chức năng mà không có loại trả về hoặc loại tham số sẽ mặc định mặc định sử dụng
20 10 GFG89:
GFG5
Hành vi này cho phép
20 10 GFG89 được sử dụng như một cửa thoát hiểm khi bạn cần trộn mã được đánh máy động và thống kê.
Tương phản hành vi của
20 10 GFG89 với hành vi của
GFG96. Tương tự như
20 10 GFG89, mọi loại là một loại phụ của
GFG96. Tuy nhiên, không giống như
20 10 GFG89, điều ngược lại không đúng:
GFG96 không phải là một kiểu con của mọi loại khác.
Điều đó có nghĩa là khi loại giá trị là
GFG96, trình kiểm tra loại sẽ từ chối hầu hết tất cả các hoạt động trên đó 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 dụng hơn là lỗi loại. Ví dụ:
GFG6
Sử dụng
GFG96 để chỉ ra rằng giá trị có thể là bất kỳ loại nào theo cách kiểu an toàn. Sử dụng
20 10 GFG89 để chỉ ra rằng một giá trị được gõ động.
Danh nghĩa so với phân nhóm cấu trúc
Ban đầu PEP 484 xác định hệ thống loại tĩnh Python là sử dụng tiểu mục danh nghĩa. Điều này có nghĩa là một lớp
def greeting[name: str] -> str: return 'Hello ' + name04 được cho phép trong đó một lớp
def greeting[name: str] -> str: return 'Hello ' + name05 được mong đợi nếu và chỉ khi
def greeting[name: str] -> str: return 'Hello ' + name04 là một lớp con của
def greeting[name: str] -> str: return 'Hello ' + name05.PEP 484 defined the Python static type system as using nominal subtyping. This means that a class
def greeting[name: str] -> str: return 'Hello ' + name04 is allowed where a class
def greeting[name: str] -> str: return 'Hello ' + name05 is expected if and only if
def greeting[name: str] -> str: return 'Hello ' + name04 is a subclass of
def greeting[name: str] -> str: return 'Hello ' + name05.
Yêu cầu này trước đây cũng áp dụng cho các lớp cơ sở trừu tượng, chẳng hạn như
def greeting[name: str] -> str: return 'Hello ' + name08. Vấn đề với phương pháp này là một lớp phải được đánh dấu rõ ràng để hỗ trợ họ, điều này không có âm thanh và không giống như những gì người ta thường làm trong mã python được gõ động một cách tự động. Ví dụ, điều này phù hợp với PEP 484:PEP 484:
GFG7
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 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 def greeting[name: str] -> str:
return 'Hello ' + name
09 được coi là một kiểu con của cả hai trình kiểm tra loại tĩnh. Điều này được gọi là phân nhóm cấu trúc [hoặc gõ vịt tĩnh]: allows to solve this problem by allowing users to write the above code without explicit base classes in the class definition, allowing
def greeting[name: str] -> str: return 'Hello ' + name09 to be implicitly considered a subtype of both
def greeting[name: str] -> str: return 'Hello ' + name10 and
def greeting[name: str] -> str: return 'Hello ' + name11 by static type checkers. This is known as structural subtyping [or static duck-typing]:
GFG8
Hơn nữa, bằng cách phân lớp một lớp đặc biệt
GFG00, người dùng có thể xác định các giao thức tùy chỉnh mới để thưởng thức hoàn toàn phân nhóm cấu trúc [xem các ví dụ bên dưới].
Nội dung mô -đun
Các mô -đun xác định các lớp, chức năng và trang trí sau đây.
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
20 10 GFG93 để hỗ trợ các biến loại bên trong
def greeting[name: str] -> str: return 'Hello ' + name14. 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ợ
def greeting[name: str] -> str: return 'Hello ' + name14.
Các loại dự phòng không được chấp nhận kể từ Python 3.9 nhưng không có cảnh báo phản đối nào sẽ được đưa ra bởi thông dịch viên. Dự kiến các 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 sẽ được xóa khỏi mô -đun
20 10 GFG98 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, Generics Generics trong các bộ sưu tập tiêu chuẩn.PEP 585—Type Hinting Generics In Standard Collections.
Đặc biệt gõ nguyên thủy
Các loại đặc biệt
Chúng có thể được sử dụng dưới dạng các loại trong các chú thích và không hỗ trợ
def greeting[name: str] -> str: return 'Hello ' + name14. gõ.Any¶
Loại đặc biệt chỉ ra một loại không bị ràng buộc.
Mỗi loại tương thích với
20 10 GFG
89.20 10 GFG
89 tương thích với mọi loại.
Đã thay đổi trong phiên bản 3.11: 20
10
GFG
89 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 kiểm tra loại với các lớp có thể loại vịt ở bất cứ đâu hoặc rất năng động.
20 10 GFG89 can now be used as a base class. This can be useful for avoiding type checker errors with classes that can duck type anywhere or are highly dynamic. Gõ.LiteralString¶
Loại đặc biệt chỉ bao gồm các chuỗi theo nghĩa đen. Một chuỗi theo nghĩa đen tương thích với
GFG19, như một
GFG19 khác, nhưng một đối tượng được gõ chỉ là
20 10 GFG96 thì không. Một chuỗi được tạo bằng cách soạn thảo các đối tượng gy ____ 219 cũng được chấp nhận dưới dạng
GFG19.
Example:
GFG9
Điều này rất hữu ích cho các API nhạy cảm trong đó các chuỗi do người dùng tạo tùy ý có thể tạo ra vấn đề. Ví dụ, hai trường hợp trên đó tạo ra lỗi kiểm tra loại có thể dễ bị tấn công SQL.
Xem PEP 675 để biết thêm chi tiết.PEP 675 for more details.
Mới trong phiên bản 3.11.
gõ. Không bao giờNever¶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ề:
def greeting[name: str] -> str: return 'Hello ' + name0
Mới trong phiên bản 3.11: Trên các phiên bản Python cũ hơn, def greeting[name: str] -> str:
return 'Hello ' + name
26 có thể được sử dụng để thể hiện cùng một khái niệm. def greeting[name: str] -> str:
return 'Hello ' + name
27 đã được thêm vào để làm cho ý định có ý nghĩa rõ ràng hơn.On older Python
versions,
def greeting[name: str] -> str: return 'Hello ' + name26 may be used to express the same concept.
def greeting[name: str] -> str: return 'Hello ' + name27 was added to make the intended meaning more explicit. Gõ.Noreturn¶NoReturn¶
Loại đặc biệt chỉ ra rằng một hàm không bao giờ trả về. Ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name1
def greeting[name: str] -> str: return 'Hello ' + name26 cũng có thể được sử dụng như một loại dưới cùng, một loại không có giá trị. Bắt đầu trong Python 3.11, loại
def greeting[name: str] -> str: return 'Hello ' + name27 nên được sử dụng cho khái niệm này. Loại kiểm tra nên đối xử với hai người tương đương.
Mới trong phiên bản 3.5.4.
Mới trong phiên bản 3.6.2.
gõ.SelfSelf¶Loại đặc biệt để đại diện cho lớp kèm theo hiện tại. Ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name2
Chú thích này tương đương về mặt ngữ nghĩa với những điều sau đây, mặc dù theo kiểu cô đọng hơn:
def greeting[name: str] -> str: return 'Hello ' + name3
Nói chung nếu một cái gì đó hiện đang tuân theo mô hình của:
def greeting[name: str] -> str: return 'Hello ' + name4
Bạn nên sử dụng
GFG18 làm cuộc gọi đến
def greeting[name: str] -> str: return 'Hello ' + name31 sẽ có
def greeting[name: str] -> str: return 'Hello ' + name32 dưới dạng loại trả về chứ không phải
def greeting[name: str] -> str: return 'Hello ' + name33.
Các trường hợp sử dụng phổ biến khác bao gồm:
def greeting[name: str] -> str: return 'Hello ' + name
34 được sử dụng làm hàm tạo thay thế và các phiên bản trả về của tham sốdef greeting[name: str] -> str: return 'Hello ' + name
35.Chú thích một phương thức
def greeting[name: str] -> str: return 'Hello ' + name
36 trả về bản thân.
Xem PEP 673 để biết thêm chi tiết.PEP 673 for more details.
Mới trong phiên bản 3.11.
gõ.typealias¶TypeAlias¶Chú thích đặc biệt để tuyên bố rõ ràng một bí danh. Ví dụ:type alias. For example:
def greeting[name: str] -> str: return 'Hello ' + name5
Xem PEP 613 để biết thêm chi tiết về các bí danh loại rõ ràng.PEP 613 for more details about explicit type aliases.
Mới trong phiên bản 3.10.
Mẫu đặc biệt
Chúng có thể được sử dụng làm loại trong các chú thích bằng cách sử dụng
def greeting[name: str] -> str: return 'Hello ' + name14, mỗi loại có một cú pháp duy nhất. Gõ.Tuple¶Tuple¶
Loại tuple;
def greeting[name: str] -> str: return 'Hello ' + name38 là loại tuple của hai mục với mục đầu tiên của loại X và loại thứ hai của loại Y. Loại tuple trống có thể được viết là
def greeting[name: str] -> str: return 'Hello ' + name39.
Ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name40 là một bộ của hai yếu tố tương ứng với các biến loại T1 và T2.
def greeting[name: str] -> str: return 'Hello ' + name41 là một bộ dữ liệu của int, float và một chuỗi.
Để chỉ định một tuple có độ dài thay đổi của loại đồng nhất, hãy sử dụng Ellipsis theo nghĩa đen, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name42. Một đơn giản
def greeting[name: str] -> str: return 'Hello ' + name43 tương đương với
def greeting[name: str] -> str: return 'Hello ' + name44, và lần lượt
def greeting[name: str] -> str: return 'Hello ' + name45. gõ.Union¶Union¶
Loại công đoàn;
def greeting[name: str] -> str: return 'Hello ' + name46 tương đương với
GFG08 và có nghĩa là X hoặc Y.
Để xác định một liên minh, hãy sử dụng, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name48 hoặc tốc ký
def greeting[name: str] -> str: return 'Hello ' + name49. Sử dụng tốc ký đó được khuyến khích. Thông tin chi tiết:
Các đối số phải là loại và phải có ít nhất một.
Các công đoàn của các công đoàn bị san phẳng, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name
6Các công đoàn của một cuộc tranh luận duy nhất biến mất, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name
7Các đối số dự phòng được bỏ qua, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name
8Khi so sánh các công đoàn, thứ tự đối số bị bỏ qua, ví dụ:
def greeting[name: str] -> str: return 'Hello ' + name
9Bạn không thể phân lớp hoặc khởi tạo
20 10 GFG
90.Bạn không thể viết
def greeting[name: str] -> str: return 'Hello ' + name
51.
Đã thay đổi trong phiên bản 3.7: Don Tiết loại bỏ các lớp con rõ ràng khỏi các công đoàn khi chạy.Don’t remove explicit subclasses from unions at runtime.
gõ.optional¶Optional¶Loại tùy chọn.
def greeting[name: str] -> str: return 'Hello ' + name52 tương đương với
def greeting[name: str] -> str: return 'Hello ' + name53 [hoặc
def greeting[name: str] -> str: return 'Hello ' + name54].
Lưu ý rằng đây không phải là khái niệm giống như một đối số tùy chọn, đó là một đối số có mặc định. Một đối số tùy chọn với mặc định không yêu cầu vòng loại
def greeting[name: str] -> str: return 'Hello ' + name55 về 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]]0
Mặt khác, nếu được phép một giá trị rõ ràng của
GFG23, việc sử dụng
def greeting[name: str] -> str: return 'Hello ' + name55 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]]1
Đã thay đổi trong phiên bản 3.10: Tùy chọn hiện có thể được viết là def greeting[name: str] -> str:
return 'Hello ' + name
53. Xem biểu thức loại công đoàn.Optional can now be written as
def greeting[name: str] -> str: return 'Hello ' + name53. See union type expressions. Gõ.Callable¶
Loại có thể gọi được;
def greeting[name: str] -> str: return 'Hello ' + name59 là một hàm của [int] -> str.
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à loại trả về. Danh sách đối số phải là danh sách các loại hoặc dấu chấm lửng; Loại trả về phải là một loại duy nhất.
Không có cú pháp để chỉ ra các đối số từ khóa hoặc tùy chọn; Các loại chức năng như vậy hiếm khi được sử dụng làm loại gọi lại.
GFG52 [Ellipsis theo nghĩa đen] có thể được sử dụng để gõ Gợi ý có thể gọi được lấy bất kỳ số lượng đối số nào và trả lại
def greeting[name: str] -> str: return 'Hello ' + name61. Một đơn vị
20 10 GFG91 tương đương với
def greeting[name: str] -> str: return 'Hello ' + name63, và lần lượt
def greeting[name: str] -> str: return 'Hello ' + name64.
Các thiết bị gọi lấy các thiết bị 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
GFG11. Ngoài ra, nếu có thể gọi được thêm hoặc xóa các đối số khỏi các loại gọi khác, toán tử
GFG12 có thể được sử dụng. Họ lần lượt nhận dạng
GFG55 và
GFG56. gõ.concatenate¶Concatenate¶
Được sử dụng với
20 10 GFG91 và
GFG11 để nhập chú thích một thứ tự cao hơn có thể gọi có thể thêm, xóa hoặc biến đổi các tham số của một người khác có thể gọi được. Việc sử dụng ở dạng
def greeting[name: str] -> str: return 'Hello ' + name71.
GFG12 hiện chỉ hợp lệ khi được sử dụng làm đối số đầu tiên cho
20 10 GFG91. Tham số cuối cùng của
GFG12 phải là
GFG11 hoặc Ellipsis [
def greeting[name: str] -> str: return 'Hello ' + name76].
Ví dụ, để chú thích một người trang trí
def greeting[name: str] -> str: return 'Hello ' + name77 cung cấp
def greeting[name: str] -> str: return 'Hello ' + name78 cho chức năng được trang trí,
GFG12 có thể được sử dụng để chỉ ra rằng
def greeting[name: str] -> str: return 'Hello ' + name77 mong đợi một cuộc gọi có thể gọi được trong một
def greeting[name: str] -> str: return 'Hello ' + name81 là đối số đầu tiên và trả về một dấu hiệu có thể gọi với một loại khác nhau. Trong trường hợp này,
GFG11 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 đượ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]]2
Mới trong phiên bản 3.10.
Xem thêm
PEP 612 - Các biến đặc tả tham số [PEP giới thiệu
GFG
11 vàGFG
12]. – Parameter Specification Variables [the PEP which introducedGFG
11 andGFG
12].GFG
11 và20 10 GFG
91.
Một biến được chú thích bằng
def greeting[name: str] -> str: return 'Hello ' + name87 có thể chấp nhận giá trị loại
def greeting[name: str] -> str: return 'Hello ' + name87. Ngược lại, một biến được chú thích bằng
def greeting[name: str] -> str: return 'Hello ' + name89 có thể chấp nhận các giá trị là bản thân các lớp - cụ thể, nó sẽ chấp nhận đối tượng lớp của
def greeting[name: str] -> str: return 'Hello ' + name87. 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]]3
Lưu ý rằng
def greeting[name: str] -> str: return 'Hello ' + name89 là Covariant:
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Thực tế là
def greeting[name: str] -> str: return 'Hello ' + name89 là hiệp phương sai ngụ ý rằng tất cả các lớp con của
def greeting[name: str] -> str: return 'Hello ' + name87 nên thực hiện cùng một chữ ký của hàm tạo và chữ ký phương pháp lớp là
def greeting[name: str] -> str: return 'Hello ' + name87. Trình kiểm tra loại nên gắn cờ vi phạm này, nhưng cũng nên cho phép các cuộc gọi xây dựng trong các lớp con phù hợp với các cuộc gọi xây dựng trong lớp cơ sở được chỉ định. Làm thế nào 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 sửa đổi trong tương lai của PEP 484.PEP 484.
Các tham số pháp lý duy nhất cho
def greeting[name: str] -> str: return 'Hello ' + name95 là các lớp,
20 10 GFG89, các biến loại và các công đoàn của bất kỳ loại nào trong số này. Ví dụ:type variables, and unions of any of these types. For example:
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]5
def greeting[name: str] -> str: return 'Hello ' + name97 tương đương với
def greeting[name: str] -> str: return 'Hello ' + name95, lần lượt tương đương với
def greeting[name: str] -> str: return 'Hello ' + name99, là gốc của hệ thống phân cấp Metaclass Python.
Mới trong phiên bản 3.5.2.
Gõ.Literal¶Literal¶Một loại có thể được sử dụng để chỉ ra các trình kiểm tra loại rằng biến hoặc tham số chức năng tương ứng có giá trị tương đương với nghĩa đen được cung cấp [hoặc một trong một số nghĩa đen]. 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]]6
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 không thể được phân nhóm. Trong thời gian chạy, một giá trị tùy ý được cho phép là đối số loạ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]]00, nhưng người kiểm tra loại có thể áp đặt các hạn chế. Xem PEP 586 để biết thêm chi tiết về các loại nghĩa đen.PEP 586 for more details about literal types.
Mới trong phiên bản 3.8.
Đã thay đổi trong phiên bản 3.9.1: GFG
03 hiện đã khử trùng các tham số. So sánh bình đẳng của các đối tượng GFG
03 không còn phụ thuộc vào thứ tự. Các đối tượng GFG
03 hiện sẽ tăng ngoại lệ Vector = list[float]
def scale[scalar: float, vector: Vector] -> Vector:
return [scalar * num for num in vector]
# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
05 trong quá trình so sánh bình đẳng nếu một trong các tham số của chúng không thể băm.
GFG03 now de-duplicates parameters. Equality comparisons of
GFG03 objects are no longer order dependent.
GFG03 objects will now raise 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]]05 exception during equality comparisons if one of their parameters are not hashable. Gõ.Classvar¶ClassVar¶
Loại cấu trúc đặc biệt để đánh dấu các biến lớp.
Như được giới thiệu trong PEP 526, một chú thích thay đổi được bọc trong classvar chỉ ra rằng một thuộc tính nhất định được dự định sẽ được sử dụng làm biến lớp và không nên được đặt trên các trường hợp của lớp đó. Cách sử dụng:PEP 526, a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class. Usage:
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]7
20 10 GFG99 chỉ chấp nhận các loại và không thể được đăng ký thêm.
20 10 GFG99 không phải là một lớp và không nên được sử dụng với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]08 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]]09.
20 10 GFG99 không thay đổi hành vi thời gian chạy Python, nhưng nó có thể được sử dụng bởi các trình kiểm tra loại bên thứ ba. Ví dụ: Trình kiểm tra loại có thể gắn cờ mã sau 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]]8
Mới trong phiên bản 3.5.3.
gõ.final¶Final¶Một cấu trúc gõ đặc biệt để chỉ ra để nhập trình kiểm tra 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]]9
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.PEP 591 for more details.
Mới trong phiên bản 3.8.
Đã thay đổi trong phiên bản 3.9.1:GFG03 hiện đã khử trùng các tham số. So sánh bình đẳng của các đối tượng
GFG03 không còn phụ thuộc vào thứ tự. Các đối tượng
GFG03 hiện sẽ tăng ngoại lệ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]05 trong quá trình so sánh bình đẳng nếu một trong các tham số của chúng không thể băm.Required¶ typing.NotRequired¶
Gõ.Classvar¶
Loại cấu trúc đặc biệt để đánh dấu các biến lớp.PEP 655 for more details.
Như được giới thiệu trong PEP 526, một chú thích thay đổi được bọc trong classvar chỉ ra rằng một thuộc tính nhất định được dự định sẽ được sử dụng làm biến lớp và không nên được đặt trên các trường hợp của lớp đó. Cách sử dụng:
20 10 GFG99 chỉ chấp nhận các loại và không thể được đăng ký thêm.Annotated¶
20 10 GFG99 không phải là một lớp và không nên được sử dụng với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]08 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]]09.
20 10 GFG99 không thay đổi hành vi thời gian chạy Python, nhưng nó có thể được sử dụng bởi các trình kiểm tra loại bên thứ ba. Ví dụ: Trình kiểm tra loại có thể gắn cờ mã sau là lỗi:PEP 593 [
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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], to decorate existing types with context-specific metadata [possibly multiple pieces of it, as
GFG07 is variadic]. Specifically, a type
GFG60 can be annotated with metadata
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 via the typehint
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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. This metadata can be used for either static analysis or at runtime. If a library [or tool] encounters a typehint
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 and has no special logic for metadata
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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, it should ignore it and simply treat the type as
GFG60. Unlike the
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]21 functionality that currently exists in the
20 10 GFG98 module which completely disables typechecking annotations on a function or a class, the
GFG07 type allows for both static typechecking of
GFG60 [which can safely ignore
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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] together with runtime access to
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]16 within a specific application.
Mới trong phiên bản 3.5.3.
gõ.final¶
Một cấu trúc gõ đặc biệt để chỉ ra để nhập trình kiểm tra 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ụ:
Vì loại
GFG07 cho phép bạn đặt một số chú thích giống nhau [hoặc khác nhau] trên bất kỳ nút nào, nên các công cụ hoặc thư viện tiêu thụ các chú thích đó chịu trách nhiệm đối phó với 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:
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
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]]31 đế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]]32 cho phép người ta truy cập vào các chú thích bổ sung khi chạy.
Các chi tiết của cú pháp:
Đối số đầu tiên của
GFG
07 phải là loại hợp lệNhiều chú thích loại được hỗ trợ [
GFG
07 hỗ trợ các đối số variadic]: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: ...
1GFG
07 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]]
36 không hợp lệ]Thứ tự của các chú thích được bảo tồn và các vấn đề để kiểm tra bình đẳ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: ...
2Các loại
GFG
07 lồng nhau được làm phẳng, với siêu dữ liệu được đặt hàng bắt đầu với chú thích trong cù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: ...
3Chú thích trùng lặp không được xó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: ...
4GFG
07 có thể được sử dụng với bí danh lồng nhau và chung chung: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
Mới trong phiên bản 3.9.
gõ.typeguard¶TypeGuard¶Biểu mẫu gõ đặc biệt được sử dụng để chú thích loại trả về của chức năng bảo vệ loại do người dùng xác định.
GFG15 chỉ chấp nhận một đối số loại duy nhất. Trong thời gian chạy, các chức năng được đánh dấu theo cách này sẽ trả về Boolean.
GFG15 nhằm mục đích thu hẹp loại lợi ích - Một kỹ thuật được sử dụng bởi các trình kiểm tra loại tĩnh để xác định một loại biểu thức chính xác hơn trong luồng mã chương trình. Thông thường 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 vào một khối mã. Biểu thức có điều kiện ở đây đôi khi được gọi là một người bảo vệ kiểu người khá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: ...6
Đôi khi sẽ thuận tiện khi sử dụng chức năng Boolean do người dùng định nghĩa làm người bảo vệ loại. Một chức năng như vậy nên sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 làm loại trả về của nó để cảnh báo các trình kiểm tra loại tĩnh cho ý đị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]]42 nói với trình kiểm tra loại tĩnh rằng đối với một hàm đã cho:
Giá trị trả lại 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]]
43, loại đối số của nó là loại bên trongGFG
15.
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: ...7
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]]45 là phương thức lớp hoặc phiên bản, thì loại trong
GFG15 bản đồ theo loại tham số thứ hai sau
def greeting[name: str] -> str: return 'Hello ' + name35 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]]48.
Nói tóm lại, mẫ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]]49, 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]]50 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]]43, 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]]52 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]]53 đế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]]54.
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]]54 không cần phải là một 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]]53 - nó thậm chí có thể là một hình thức 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]]57 đế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]]58 mặc dù sau này không phải là một loại phụ 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]]59 là bất biến. Trách nhiệm của việc viết bộ bảo vệ loại an toàn loại được để lại cho người dùng.
GFG15 cũng hoạt động với các biến loại. Xem PEP 647 để biết thêm chi tiết.PEP 647 for more details.
Mới trong phiên bản 3.10.
Xây dựng các loại chung chung
Chúng không được sử dụng trong các chú thích. Họ đang xây dựng các khối để tạo ra các loại chung.
classtyping.generic¶typing.Generic¶Lớp cơ sở trừu tượng cho các loại chung.
Một loại chung thường được khai báo bằng cách kế thừa từ một khởi tạo của lớp này với một hoặc nhiều biến loại. Ví dụ: loại ánh xạ chung có thể được định nghĩa 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: ...8
Lớp này sau đó có thể được sử dụng như 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: ...9 classtyping.typevar¶typing.TypeVar¶
Loại biến.
Usage:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]0
Loại biến tồn tại chủ yếu vì lợi ích của người kiểm tra loại tĩnh. Chúng phục vụ như các tham số cho các loại chung cũng như các định nghĩa chức năng chung. Xem
20 10 GFG93 để biết thêm thông tin về các loại chung. Chức năng chung hoạt động như sau:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]1
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ể bị ràng buộc và bị ràng buộc.
Các biến loại ràng buộc và các biến loại bị ràng buộc có ngữ nghĩa khác nhau theo nhiều cách quan trọng. Sử dụng biến loại ràng buộc có nghĩa là
20 10 GFG92 sẽ được giải quyết bằng cách sử dụng loại cụ thể nhất có thể:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]2
Các biến loại có thể được liên kết với các loại cụ thể, các loại trừu tượng [ABC hoặc giao thức] và thậm chí cả các công đoàn của các loại:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]3
Tuy nhiên, sử dụng một biến loại bị ràng buộc có nghĩa là
20 10 GFG92 chỉ có thể được giải quyết là chính xác là một trong những ràng buộc đã đưa ra:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]4
Trong thời gian 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]]64 sẽ tăng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]05. Nó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]]08 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]]09 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 phương sai hoặc contravariant bằng cách 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]]68 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]]69. Xem PEP 484 để biết thêm chi tiết. Theo mặc định, các biến loại là bất biến.PEP 484 for more details. By default, type variables are invariant. classtyping.typevartuple¶typing.TypeVarTuple¶
Loại biến tuple. Một hình thức chuyên dụ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]]70 cho phép các chất generic variadic.
Một 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 loại biến loại cho phép tham số hóa với số lượng loại tùy ý bằng cách hoạt động giống như một số lượng biến tùy ý được bọc trong một tuple. Ví dụ:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]5
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]]71 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]]72. Về mặt khái niệm, bạn có thể nghĩ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]73 như một bộ biến của cá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]]74.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]76, 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]]77. .
Loại Tuples biến phải luôn được giải nén. Điều này giúp phân biệt các bộ dữ liệu biến loại với các biến loại bình thường:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]6
Loại Tuples biến có thể được sử dụng trong cùng một bối cảnh với các biến loại bình thường. Ví dụ: trong các định nghĩa lớp, đối số và loại trả về:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]7
Loại Tuples biến có thể được kết hợp vui vẻ với các biến loại bình thường:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]8
Tuy nhiên, lưu ý rằng nhiều nhất là một loại biến có thể xuất hiện trong một danh sách các đối số hoặc loại loại loại:
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]9
Cuối cùng, một loại biến loại chưa đóng gói có thể được sử dụng làm chú thích 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]]80:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]0
Trái ngược với các chú thích không được đóng gó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]]80 - 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]]82, trong đó sẽ chỉ định rằng tất cả các đối số là
GFG26 -
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]80. Ở đâ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]]80 được chuyể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]]87 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]]88.
Xem PEP 646 để biết thêm chi tiết về các bộ dữ liệu biến loại.PEP 646 for more details on type variable tuples.
Mới trong phiên bản 3.11.
gõ.unpack¶Unpack¶Một toán tử đánh máy đánh dấu một đối tượng 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]]71 trên
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]90 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]]78 để đánh dấu loại biến loại loại đã được giải nén:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]1
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]]78 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]]71 trong bối 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]]78 đang được sử dụng rõ ràng trong các phiên bản cũ của Python, 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]]71 không thể được sử dụng ở một số nơi nhất định:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]2
Mới trong phiên bản 3.11.
gõ.unpack¶typing.ParamSpec[name, *, bound=None, covariant=False, contravariant=False]¶Một toán tử đánh máy đánh dấu một đối tượng 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]]71 trên
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]90 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]]78 để đánh dấu loại biến loại loại đã được giải nén:
Usage:
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]]78 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]]71 trong bối 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]]78 đang được sử dụng rõ ràng trong các phiên bản cũ của Python, 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]]71 không thể được sử dụng ở một số nơi nhất định:
classtyping.paramspec [name, *, bound = none, covariant = false
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]3
Biến đặc tả tham số. Một phiên bản chuyên dụ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]]96.
Các biến đặc tả tham số tồn tại chủ yếu vì lợi ích của bộ kiểm tra loại tĩnh. Chúng được sử dụng để chuyển tiếp các loại tham số của một người có thể gọi cho một người khác có thể gọi - một mẫu thường được tìm thấy trong các chức năng và bộ trang trí bậc cao. Chúng chỉ hợp lệ khi được sử dụng trong
GFG
12 hoặc là đối số đầu tiên đối với20 10 GFG
91 hoặc làm tham số cho thuốc generic do người dùng xác định. Xem20 10 GFG
93 để biết thêm thông tin về các loại chung.Ví dụ: để thêm ghi nhật ký cơ bản vào một chức năng, người ta có thể tạo Trình trang 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: ...
00 để ghi nhật ký các cuộc gọi chức năng. Biến đặc tả tham số cho biết trình kiểm tra loại rằng có thể gọi được chuyển vào trình trang trí và phần gọi mới được trả về bởi nó có các tham số loại phụ thuộc giữa các tham số:
GFG11, cách đơn giản nhất để chú thích điều này trước đây là sử dụng
20 10 GFG92 với ràng buộc
def greeting[name: str] -> str: return 'Hello ' + name63. Tuy nhiên, điều này gây ra hai vấn đề:¶ kwargs¶
Trình kiểm tra loại có thể loại kiểm tra chức nă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: ...04 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]]80 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: ...06 phải được gõ
20 10 GFG89.
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: ...08 có thể được yêu cầu trong thân máy trang 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: ...00 khi trả lại hà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: ...04 hoặc trình kiểm tra loại tĩnh phải được yêu cầu bỏ qua
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.
Args¶ Kwargs¶
Vì
GFG11 nắm bắt cả các tham số vị trí và từ khó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: ...13 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: ...14 có thể được sử dụng để chia
GFG11 thành các thành phần của 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: ...13 đại diện cho bộ dữ liệu của các 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]]80.
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 đại diện cho ánh xạ các tham số từ khóa đến 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
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: ...06. Cả hai thuộc tính đều yêu cầu tham số chú thích phải nằm trong phạm vi. Trong thời gian chạ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: ...13 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: ...14 là các trường hợp tương ứng 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: ...22 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: ...23.
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]]68 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]]69 có thể được sử dụng để khai báo các loại chung hiệp phương sai hoặc contravariant. Đối 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: ...26 cũng được chấp nhận, tương tự như
20 10 GFG92. Tuy nhiên, ngữ nghĩa thực tế của các từ khóa này vẫn chưa được quyết định.
Mới trong phiên bản 3.10.
Ghi chú – Parameter Specification Variables [the PEP which introduced
GFG
11 andGFG
12].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 ngâm.
PEP 612 - Các biến đặc tả tham số [PEP giới thiệu
GFG11 và
GFG12].
Gọ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: ...38 trên một trong hai đối tượng này sẽ trả về
GFG11 gốc:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]4
Mới trong phiên bản 3.10.
gõ.AnyStr¶AnyStr¶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: ...40 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: ...41 được định nghĩa 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: ...42.
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. Ví dụ:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]5 classtyping.protocol [chung] ¶typing.Protocol[Generic]¶
Lớp cơ sở cho các lớp giao thức. Các lớp giao thức được xác định như thế này:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]6
Các lớp như vậy chủ yếu được sử dụng với các trình kiểm tra loại tĩnh nhận ra phân nhóm cấu trúc [ví dụ: gõ vịt tĩnh], ví dụ:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]7
Xem PEP 544 để biết thêm chi tiết. Các lớp giao thức được trang trí 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: ...43 [được mô tả sau] đóng vai trò là các giao thức thời gian chạy có đầu óc đơn giản chỉ kiểm tra sự hiện diện của các thuộc tính đã cho, bỏ qua các chữ ký loại của chúng.PEP 544 for more details. Protocol classes decorated with
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: ...43 [described later] act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures.
Các lớp giao thức có thể là chung chung, ví dụ:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]8
Mới trong phiên bản 3.8.
@gõ.runtime_checkable¶typing.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
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]09. Điều này 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]]05 khi áp dụng cho một lớp không giao tiếp. Điều này cho phép kiểm tra cấu trúc có đầu óc đơn giản, rất giống với những con ngựa con của One One 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: ...47 như
def greeting[name: str] -> str: return 'Hello ' + name08. Ví dụ:
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]9
Ghi chú
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: ...43 sẽ chỉ kiểm tra sự hiện diện của các phương pháp cần thiết, không phải chữ ký loại của chúng. 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: ...50 là một lớp, do đó nó vượt qua kiểm tra
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]09 đối với
20 10 GFG91. Tuy nhiên, phương 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: ...53 chỉ tồn 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]]05 với một thông điệp nhiều thông tin hơn, do đó khiến nó không thể gọi [khởi tạ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: ...50.
Mới trong phiên bản 3.8.
@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ớiVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]09. Điều này 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]]05 khi áp dụng cho một lớp không giao tiếp. Điều này cho phép kiểm tra cấu trúc có đầu óc đơn giản, rất giống với những con ngựa con của One One 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: ...47 như
def greeting[name: str] -> str: return 'Hello ' + name08. Ví dụ:typing.NamedTuple¶
Ghi chú
Usage:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]0
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: ...43 sẽ chỉ kiểm tra sự hiện diện của các phương pháp cần thiết, không phải chữ ký loại của chúng. 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: ...50 là một lớp, do đó nó vượt qua kiểm tra
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]09 đối với
20 10 GFG91. Tuy nhiên, phương 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: ...53 chỉ tồn 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]]05 với một thông điệp nhiều thông tin hơn, do đó khiến nó không thể gọi [khởi tạ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: ...50.
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]1
Các chỉ thị đặc biệt khác
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]2
Chúng không được sử dụng trong các chú thích. Họ đang xây dựng các khối để khai báo các loại.
classtyping.namedtuple¶
Phiên bản gõ 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: ...56.
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]3
Điều này tương đương với:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]4
Để cung cấp cho trường một giá trị mặc định, bạn có thể gán cho nó trong phần thân lớp:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]5
Các trường có giá trị mặc định phải đến sau bất kỳ trường nào mà không có mặc định.Added support for PEP 526 variable annotation syntax.
Lớp kết quả có một thuộc tính bổ sung 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 đưa ra một dictor bản đồ tên trường thành các loại trường. .Added support for default values, methods, and docstrings.
Các lớp con cũng có thể có tài liệu và phương pháp:The
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: ...63 and
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 attributes are now regular dictionaries instead of instances of
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: ...65.
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:
...
61 Các lớp con có thể là chung:Removed the
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: ...63 attribute in favor of the more standard
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 attribute which has the same information.
Sử dụng tương thích ngược:Added support for generic namedtuples.
Đã thay đổi trong phiên bản 3.6: Đã thêm hỗ trợ cho cú pháp chú thích biến PEP 526.typing.NewType[name, tp]¶Đã thay đổi trong phiên bản 3.6.1: Đã thêm hỗ trợ cho các giá trị, phương thức và tài liệu mặc định.NewType. At runtime it returns an object that returns its argument when called. Usage:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]6
Đã thay đổi trong phiên bản 3.8: Các thuộc 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:
...
63 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:
...
57 hiện là từ điển thường xuyên thay vì các trường hợ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:
...
65.
Đã thay đổi trong phiên bản 3.9: Đã xóa thuộc 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:
...
63 có lợi cho thuộc 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:
...
57 tiêu chuẩn hơn có cùng thông tin.
GFG25 is now a class rather than a function. Đã thay đổi trong phiên bản 3.11: Đã thêm hỗ trợ cho Generic có tên. typing.TypedDict[dict]¶
classtyping.newtype [tên, tp] ¶
Một lớp trợ giúp để chỉ ra một loại riêng biệt cho một typechecker, xem newtype. Khi chạy, nó trả về một đối tượng trả về đối số của nó khi được gọi. Cách sử dụng:
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]7
Mới trong phiên bản 3.5.2.PEP 526,
GFG04 supports two additional equivalent syntactic forms:
Đã thay đổi trong phiên bản 3.10:
GFG
25 hiện là một lớp chứ không phải là một hàm.# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]
8classtyping.typeddict [dict] ¶
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]
9
Cấu trúc đặc biệt để thêm gợi ý loại vào từ điển. Vào thời gian chạy, nó là mộ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:
...
69 đơn giản.The keyword-argument syntax is deprecated in 3.11 and will be removed in 3.13. It may also be unsupported by static type checkers.
GFG04 tuyên bố một loại từ điển mong đợi tất cả các trường hợp của nó sẽ có một bộ khóa nhất định, trong đó mỗi khóa được liên kết với 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 nhưng chỉ được thực thi bởi người kiểm tra loại. Cách sử dụng:identifiers, for example because they are keywords or contain hyphens. Example:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass0
Để 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,
GFG04 hỗ trợ hai hình thức cú pháp tương đương bổ sung:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass1
Sử dụng mộ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: ...69 theo nghĩa đen làm đối số thứ hai:
Sử dụng từ khóa đối số:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass2
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 từ khóa-Progument được không dùng nữa trong 3.11 và sẽ bị xóa trong 3.13. Nó cũng có thể không được hỗ trợ bởi các trình kiểm tra loại tĩnh.
Các khóa riêng lẻ 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: ...85
GFG04 có thể được đánh dấu theo yêu cầu bằng cách sử dụng
GFG16:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass3
Có thể loại
GFG04 để kế thừa từ một hoặc nhiều loại
GFG04 khác bằng cách sử dụng cú pháp dựa trên lớp. Cách sử dụng:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass4
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: ...90 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]]16,
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: ...92 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: ...93. Nó tương đương với định nghĩa này:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass5
Một
GFG04 không thể kế thừa từ một lớp Non -____ 204, ngoại trừ
20 10 GFG93. Ví dụ:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass6
A
GFG04 có thể là chung:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass7
A
GFG04 có thể được nội tâm thông qua các chú thích Dicts [xem các thông chứng thực hành tốt nhất để biết thêm thông tin về các thông lệ tốt 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: ...99,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]00 và
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]01.Annotations Best Practices for more information on annotations best practices],
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,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]00, and
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]01. __toàn bộ__¶¶
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]02 đưa ra giá trị của đối 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: ...83. Thí dụ:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass8 __required_keys__¶¶
Mới trong phiên bản 3.9.
__optional_keys__¶¶from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]04 và
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]05 Trả về
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]06 Các đối tượng có chứa các khóa cần thiết và không yêu cầu, tương ứng.
Các khóa được đánh dấu bằng
GFG16 sẽ luôn xuất hiện trong
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]00 và các phím được đánh dấu bằng
GFG17 sẽ luôn xuất hiện trong
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]01.
Đối với khả năng tương thích ngược với Python 3.10 trở xuống, cũng có thể sử dụng kế thừa để khai báo cả các khóa yêu cầu và không yêu cầu trong cùng một
GFG04. Điều này được thực hiện bằng cách khai báo
GFG04 với một giá trị cho đối 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: ...83 và sau đó kế thừa từ nó trong một
GFG04 khác với giá trị khác 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: ...83:
from typing import NewType UserId = NewType['UserId', int] # Fails at runtime and does not pass type checking class AdminUserId[UserId]: pass9
Mới trong phiên bản 3.9.
__optional_keys__¶PEP 589 for more examples and detailed rules of using
GFG04.
from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
04 và from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
05 Trả về from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
06 Các đối tượng có chứa các khóa cần thiết và không yêu cầu, tương ứng.
Các khóa được đánh dấu bằng GFG
16 sẽ luôn xuất hiện trong from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
00 và các phím được đánh dấu bằng GFG
17 sẽ luôn xuất hiện trong from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
01.Added support for marking individual keys as
GFG16 or
GFG17. See PEP 655.
Đối với khả năng tương thích ngược với Python 3.10 trở xuống, cũng có thể sử dụng kế thừa để khai báo cả các khóa yêu cầu và không yêu cầu trong cùng một GFG
04. Điều này được thực hiện bằng cách khai báo GFG
04 với một giá trị cho đối 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:
...
83 và sau đó kế thừa từ nó trong một GFG
04 khác với giá trị khác 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:
...
83:Added support for generic
GFG04s.
Xem PEP 589 để biết thêm ví dụ và quy tắc chi tiết sử dụng GFG
04.
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àGFG16 hoặc
GFG17. Xem PEP 655. typing.Dict[dict, MutableMapping[KT, VT]]¶
Đã thay đổi trong phiên bản 3.11: Đã thêm hỗ trợ cho các
GFG04 chung.
Bộ sưu tập bê tông chung chung
20 10 GFG00 Tương ứng với các loại tích hợptyping.List[list, MutableSequence[T]]¶
classtyping.dict [dict, mutablemapping [kt, vt]] ¶
Một phiên bản chung 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: ...69. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nó được ưu tiên sử dụng một loại bộ sưu tập trừu tượng như
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]21.
20 10 GFG01 Loại này có thể được sử dụng như sau:typing.Set[set, MutableSet[T]]¶
classtyping.list [Danh sách, Mutablesequence [T]] ¶
Phiên bản chung 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]]59. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nó được ưu tiên sử dụng một loại bộ sưu tập trừu tượng, chẳng hạn như
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]23 hoặc
def greeting[name: str] -> str: return 'Hello ' + name08.typing.FrozenSet[frozenset, AbstractSet[T_co]]¶
Loại này có thể được sử dụng như sau:
classtyping.set [set, mutableset [t]] ¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]25. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nó được ưu tiên sử dụng một loại bộ sưu tập trừu tượng, chẳng hạn như
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]26.
classtyping.frozenset [Frozenset, Abstractset [T_CO]] ¶
Một phiên bản chung củafrom typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]27. typing.DefaultDict[collections.defaultdict, MutableMapping[KT, VT]]¶
Ghi chú
def greeting[name: str] -> str:
return 'Hello ' + name
43 là một hình thức đặc biệt.
classtyping.defaultdict [Collections.DefaultDict, Mutablemapping [KT, VT]] ¶
Một phiên bản chung của from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
30.
classtyping.ordereddict [Collections.ordereddict, mutablemapping [kt, vt]] ¶
Một phiên bản chung của from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
31.
Mới trong phiên bản 3.7.2.
classtyping.chainmap [bộ sưu tập.chainmap, mutablemapping [kt, vt]] ¶typing.Counter[collections.Counter, Dict[T, int]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]32.
Một phiên bản chung của from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
31.
Mới trong phiên bản 3.7.2.
classtyping.chainmap [bộ sưu tập.chainmap, mutablemapping [kt, vt]] ¶ typing.Deque[deque, MutableSequence[T]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]32.
Một phiên bản chung của from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
31.
Mới trong phiên bản 3.7.2.
classtyping.chainmap [bộ sưu tập.chainmap, mutablemapping [kt, vt]] ¶
Một phiên bản chung củafrom typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]32. typing.IO¶ classtyping.TextIO¶ classtyping.BinaryIO¶
Mới trong phiên bản 3.5.4.
Mới trong phiên bản 3.6.1.The
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]39 namespace is deprecated and will be removed. These types should be directly imported from
20 10 GFG98 instead. classtyping.Count [com thu.typing.Pattern¶ classtyping.Match¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]33.
classtyping.deque [deque, mutablesequence [t]] ¶The
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]48 namespace is deprecated and will be removed. These types should be directly imported from
20 10 GFG98 instead.
Một phiên bản chung của from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
34.Classes
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]50 and
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]51 from
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]52 now support
def greeting[name: str] -> str: return 'Hello ' + name14. See PEP 585 and Generic Alias Type. Các loại bê tông kháctyping.Text¶
classtyping.io¶ classtyping.textio¶ classtyping.binaryio¶
Loại chung
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]35 và các lớp con của nó
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]36 và
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]37 đại diện cho các loại luồng I/O như được trả về bởi
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]38.
20 10 GFG02
Mới trong phiên bản 3.5.2.
Không dùng nữa vì 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 loại cũng không còn hỗ trợ kiểm tra loại mã Python 2. Việc loại bỏ bí danh hiện không được lên kế hoạch, nhưng người dùng được khuyến khích sử dụng 20
10
GFG
96 thay vì from typing import NewType
UserId = NewType['UserId', int]
some_id = UserId[524313]
54 bất cứ khi nào có thể.Python 2 is no longer supported, and most type checkers also no longer support type checking Python 2 code. Removal of the alias is not currently planned, but users are encouraged to use
20 10 GFG96 instead of
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]54 wherever possible.
Lớp học cơ sở trừu tượng Jo
Tương ứng với các bộ sưu tập trong ____ 547¶
classtyping.abstractset [bộ sưu tập [t_co]] ¶ typing.AbstractSet[Collection[T_co]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]62. classtyping.bytestring [trình tự [int]] ¶typing.ByteString[Sequence[int]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]63.
Loại này đại diện cho các loại
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]64,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]65 và
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]66 của chuỗi byte.
Là một tốc ký cho loại này,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]64 có thể được sử dụng để chú thích các đối số của bất kỳ loại nào được đề cập ở trên. classtyping.collection [kích thước, itable [t_co], container [t_co]] ¶ typing.Collection[Sized, Iterable[T_co], Container[T_co]]¶
Phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]68
Mới trong phiên bản 3.6.0.
classtyping.container [chung [t_co]] ¶ typing.Container[Generic[T_co]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]69. classtyping.itemsview [Bản đồView, Tóm tắt [tuple [KT_CO, VT_CO]]] ¶typing.ItemsView[MappingView, AbstractSet[tuple[KT_co, VT_co]]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]70. classtyping.keysview [Bản đồView, Abstractset [kt_co]] ¶typing.KeysView[MappingView, AbstractSet[KT_co]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]71. classtyping.mapping [bộ sưu tập [kt], chung [kt, vt_co]] ¶ typing.Mapping[Collection[KT], Generic[KT, VT_co]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]72. Loại này có thể được sử dụng như sau:
20 10 GFG03 classtyping.mappingview [kích thước] ¶typing.MappingView[Sized]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]73. classtyping.mutablemapping [ánh xạ [kt, vt]] ¶typing.MutableMapping[Mapping[KT, VT]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]74. classtyping.mutablesequence [trình tự [t]] ¶ typing.MutableSequence[Sequence[T]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]75. classtyping.mutableset [Tóm tắt [T]] ¶typing.MutableSet[AbstractSet[T]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]76. classtyping.typing.Sequence[Reversible[T_co], Collection[T_co]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]77.
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]78.
Tương ứng với các loại khác trong ____ 547¶
classtyping.iterable [chung [t_co]] ¶typing.Iterable[Generic[T_co]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]80. classtyping.iterator [itable [t_co]] ¶typing.Iterator[Iterable[T_co]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]81. classtyping.generator [iterator [t_co], chung [t_co, t_contra, v_co]] ¶typing.Generator[Iterator[T_co], Generic[T_co, T_contra, V_co]]¶
Một trình tạo có thể được chú thích bằng loại chung
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]82. Ví dụ:
20 10 GFG04
Lưu ý rằng không giống như nhiều chất generic khác trong mô -đun đánh máy,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]83 của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]84 hành xử trái ngược, không bất biến hoặc bất biến.
Nếu trình tạo của bạn chỉ mang lại các giá trị, hãy đặt
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]83 và
def greeting[name: str] -> str: return 'Hello ' + name61 thành
GFG23:
20 10 GFG05
Ngoài ra, hãy chú thích trình tạo của bạn là có loại trả về
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]88 hoặc
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]89:
20 10 GFG06 classtyping.hashable¶ typing.Hashable¶
Một bí danh đến
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]90. classtyping.Veversible [itable [t_co]] ¶ typing.Reversible[Iterable[T_co]]¶
Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]91. classtyping.sized¶typing.Sized¶
Một bí danh đến
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]92.
Lập trình không đồng bộ
classtyping.coroutine [có thể chờ đợi [v_co], chung [t_co, t_contra, v_co]] ¶typing.Coroutine[Awaitable[V_co], Generic[T_co, T_contra, V_co]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]93. Phương sai và thứ tự của các biến loại tương ứng với các biến của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]84, ví dụ:
20 10 GFG07
Mới trong phiên bản 3.5.3.
classtyping.asyncgenerator [asynciterator [t_co], chung [t_co, t_contra]] ¶typing.AsyncGenerator[AsyncIterator[T_co], Generic[T_co, T_contra]]¶Một trình tạo async có thể được chú thích bằng loại chung
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]95. Ví dụ:
20 10 GFG08
Không giống như các trình tạo bình thường, các trình tạo async không thể trả về một giá trị, do đó không có tham số loại
def greeting[name: str] -> str: return 'Hello ' + name61. Như với
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]84,
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]83 hành xử trái ngược nhau.
Nếu trình tạo của bạn chỉ mang lại các giá trị, hãy đặt
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]83 thành
GFG23:
20 10 GFG09
Ngoài ra, hãy chú thích trình tạo của bạn là có loại trả về
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]01 hoặc
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]02:
20 10 GFG10
Mới trong phiên bản 3.6.1.
classtyping.asynciterable [chung [t_co]] ¶ typing.AsyncIterable[Generic[T_co]]¶Một phiên bản chung của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]03.
Mới trong phiên bản 3.5.2.
classtyping.asynciterator [không đồng bộ [T_CO]] ¶typing.AsyncIterator[AsyncIterable[T_co]]¶Một phiên bản chung của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]04.
Mới trong phiên bản 3.5.2.
classtyping.awaitable [chung [t_co]] ¶ typing.Awaitable[Generic[T_co]]¶Một phiên bản chung của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]05.
Mới trong phiên bản 3.5.2.
Trình quản lý bối cảnh Loại
classtyping.contextmanager [chung [t_co]] ¶ typing.ContextManager[Generic[T_co]]¶Một phiên bản chung của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]06.
Mới trong phiên bản 3.5.4.
Mới trong phiên bản 3.6.0.
classtyping.container [chung [t_co]] ¶typing.AsyncContextManager[Generic[T_co]]¶Một phiên bản chung của
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]69.
Mới trong phiên bản 3.5.4.
classtyping.asynccontextmanager [chung [t_co]] ¶
Một phiên bản chung của def get_user_name[user_id: UserId] -> str:
...
# passes type checking
user_a = get_user_name[UserId[42351]]
# fails type checking; an int is not a UserId
user_b = get_user_name[-1]
07.
Mới trong phiên bản 3.6.2.
Giao thứctyping.SupportsAbs¶Các giao thức này được trang trí 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: ...43. classtyping.supportsabs¶typing.SupportsBytes¶
Một ABC với một phương pháp trừu tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]09 là hiệp phương sai trong loại trả lại của nó. classtyping.supportsbytes¶ typing.SupportsComplex¶
Một ABC với một phương pháp trừu tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]10. classtyping.supportscomplex¶ typing.SupportsFloat¶
Một ABC với một phương pháp trừu tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]11. classtyping.supportsfloat¶ typing.SupportsIndex¶
Một ABC với một phương pháp trừu tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]12.
classtyping.supportsindex¶
Một ABC với một phương pháp trừu tượngdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]13. typing.SupportsInt¶
Mới trong phiên bản 3.8.
classtyping.supportsInt¶ typing.SupportsRound¶Một ABC với một phương pháp trừu tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]14.
Chức năng và Người trang trí
gõ.cast [typ, val] ¶cast[typ, val]¶Đúc một giá trị thành một loại.
Điều này trả về giá trị không thay đổi. Đối với người kiểm tra loại 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 cứ điều gì [chúng tôi muốn điều này càng nhanh càng tốt].
gõ.assert_type [val, typ, /] ¶assert_type[val, typ, /]¶Hỏi một trình kiểm tra loại tĩnh để xác nhận rằng Val có một loại type được suy ra.
Khi trình kiểm tra loại gặp phải cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]16, nó sẽ phát ra lỗi nếu giá trị không thuộc loại được chỉ định:
20 10 GFG11
Vào thời gian 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 rất hữu ích để đảm bảo loại kiểm tra loại hiểu về một tập lệnh phù hợp với ý định của nhà phát triển:
20 10 GFG12
Mới trong phiên bản 3.11.
gõ.assert_never [arg, /] ¶assert_never[arg, /]¶Hỏi trình kiểm tra loại tĩnh để xác nhận rằng một dòng mã là không thể truy cập được.
Example:
20 10 GFG13
Ở đây, các chú thích cho phép trình kiểm tra loại suy ra 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]]52 là
GFG26 hoặc
20 10 GFG96 và cả hai tùy chọn đều được bao gồm bởi các trường hợp trước đó. Nếu một trình kiểm tra loại thấy rằng một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]20 có thể truy cập được, nó sẽ phát ra một 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]]52 thay vào đó là
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]22, trình kiểm tra loại sẽ phát ra một lỗi chỉ ra rằng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]23 là loại
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]24. Đối với một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]25 để vượt qua kiểm tra loại, loại được suy ra của đối số được truyền trong phải là loại dưới cùng,
def greeting[name: str] -> str: return 'Hello ' + name27 và không có gì khác.
Trong thời gian chạy, điều này ném một ngoại lệ khi được gọi.
Mới trong phiên bản 3.11.
gõ.assert_never [arg, /] ¶reveal_type[obj, /]¶Hỏi trình kiểm tra loại tĩnh để xác nhận rằng một dòng mã là không thể truy cập được.
Ở đây, các chú thích cho phép trình kiểm tra loại suy ra 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]]52 là
GFG26 hoặc
20 10 GFG96 và cả hai tùy chọn đều được bao gồm bởi các trường hợp trước đó. Nếu một trình kiểm tra loại thấy rằng một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]20 có thể truy cập được, nó sẽ phát ra một 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]]52 thay vào đó là
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]22, trình kiểm tra loại sẽ phát ra một lỗi chỉ ra rằng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]23 là loại
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]24. Đối với một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]25 để vượt qua kiểm tra loại, loại được suy ra của đối số được truyền trong phải là loại dưới cùng,
def greeting[name: str] -> str: return 'Hello ' + name27 và không có gì khác.
20 10 GFG14
Trong thời gian chạy, điều này ném một ngoại lệ khi được gọi.
gõ.reveal_type [obj, /] ¶
20 10 GFG15
Tiết lộ loại tĩnh suy ra của một biểu thức.
Khi trình kiểm tra loại tĩnh bắt gặp một cuộc gọi đến chức năng này, nó sẽ phát ra một chẩn đoán với loại đối số. Ví dụ:
20 10 GFG16
Mới trong phiên bản 3.11.
gõ.assert_never [arg, /] ¶typing.dataclass_transform¶Hỏi trình kiểm tra loại tĩnh để xác nhận rằng một dòng mã là không thể truy cập được.
Ở đây, các chú thích cho phép trình kiểm tra loại suy ra 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]]52 là
GFG26 hoặc
20 10 GFG96 và cả hai tùy chọn đều được bao gồm bởi các trường hợp trước đó. Nếu một trình kiểm tra loại thấy rằng một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]20 có thể truy cập được, nó sẽ phát ra một 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]]52 thay vào đó là
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]22, trình kiểm tra loại sẽ phát ra một lỗi chỉ ra rằng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]23 là loại
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]24. Đối với một cuộc gọi đến
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]25 để vượt qua kiểm tra loại, loại được suy ra của đối số được truyền trong phải là loại dưới cùng,
def greeting[name: str] -> str: return 'Hello ' + name27 và không có gì khác.
20 10 GFG17
Trong thời gian chạy, điều này ném một ngoại lệ khi được gọi.
20 10 GFG18
gõ.reveal_type [obj, /] ¶
20 10 GFG19
Tiết lộ loại tĩnh suy ra của một biểu thức.
Khi trình kiểm tra loại tĩnh bắt gặp một cuộc gọi đến chức năng này, nó sẽ phát ra một chẩn đoán với loại đối số. Ví dụ:
Điều này có thể hữu ích khi bạn muốn gỡ lỗi cách người 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:
Hầu hết các trình kiểm tra loại hỗ trợ
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
27 ở bất cứ đâu, ngay cả khi tên không được nhập từ20 10 GFG
98. Nhập tên từ20 10 GFG
98 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.Khi chạy, chức năng này in loại thời gian chạy của đối số của nó thành stderr và trả về nó không thay đổi:
@Gõ.DataClass_Transform¶
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
30 có thể được sử dụng để trang trí một lớp, metaclass hoặc một chức năng tự nó là một người trang trí. Sự hiện diện củadef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
31 cho biết một trình kiểm tra loại tĩnh rằng đối tượng được trang trí thực hiện thời gian chạy ma thuật, biến đổi một lớp, đưa ra các hành vi giống như ____ 732.
Ví dụ sử dụng với chức năng trang trí:
Trên một lớp cơ sở:
Trên một metaclass:
Các lớp
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
33 được xác định ở trên sẽ được xử lý bởi các trình kiểm tra loại tương tự như các lớp được tạo bằngdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
34. Ví dụ: trình kiểm tra loại sẽ giả sử các lớp này có các phương thứcdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
35 chấp nhậndef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
36 và20 10 GFG
95.def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
68 là bí danh chodef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
65.def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
45 cho biết 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]]
43, trường sẽ chỉ dành cho từ khóa. Nếufrom 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, nó sẽ không chỉ là từ khóa. Nếu không xác định, giá trị của tham sốdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
45 trên đối tượng được trang trí bằngdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
30 sẽ được sử dụng hoặc nếu điều đó không được xác định, giá trị củadef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
56 trêndef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
30 sẽ được sử dụng.def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
77 cung cấp một tên thay thế cho trường. Tên thay thế này được sử dụng trong phương phápdef get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]
35 tổng hợp.
Trong thời gian chạy, người trang trí này ghi lại các lập luận của nó trong thuộc tính
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]79 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.PEP 681 for more details.
Mới trong phiên bản 3.11.
@Gõ.Overload¶typing.overload¶Trình trang trí
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]80 cho phép mô tả các chức năng và phương pháp hỗ trợ nhiều kết hợp khác nhau của các loại đối số. Một loạt các định nghĩa được trang trí ____ 780 phải được theo sau bởi chính xác một định nghĩa không được trang trí 780 [cho cùng một hàm/phương thức]. Các định nghĩa được trang trí ____ 780 chỉ vì 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í 780, trong khi loại sau được sử dụng trong thời gian chạy nhưng nên bị bỏ qua bởi trình kiểm tra loại. Trong thời gian chạy, gọi trực tiếp chức năng được trang trí ____ 780 sẽ tăng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]86. Một ví dụ về quá tải cung cấp một loại chính xác hơn có thể được thể hiện bằng cách sử dụng một liên kết hoặc biến loại:
20 10 GFG20
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.PEP 484 for more details and comparison with other typing semantics.
Đã thay đổi trong phiên bản 3.11: Các chức năng quá tải hiện có thể được nội tâm khi chạy bằng cách sử dụng def get_user_name[user_id: UserId] -> str:
...
# passes type checking
user_a = get_user_name[UserId[42351]]
# fails type checking; an int is not a UserId
user_b = get_user_name[-1]
87.Overloaded functions can now be introspected at runtime using
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]87. gõ.get_overloads [func] ¶get_overloads[func]¶
Trả về một chuỗi các định nghĩa được trang trí ____ 780 cho FUNC. Func là đối tượng chức năng để thực hiện chức năng quá tải. Ví dụ: với định nghĩa của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]89 trong tài liệu cho
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]80,
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]91 sẽ trả về một chuỗi ba đối tượng chức năng cho ba quá tải được xác định. Nếu được gọi trên một chức năng không có quá tải,
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]87 trả về một chuỗi trống.
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]87 có thể được sử dụng để hướng nội một chức năng quá tải trong thời gian chạy.
Mới trong phiên bản 3.11.
@Gõ.Overload¶clear_overloads[]¶Trình trang trí
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]80 cho phép mô tả các chức năng và phương pháp hỗ trợ nhiều kết hợp khác nhau của các loại đối số. Một loạt các định nghĩa được trang trí ____ 780 phải được theo sau bởi chính xác một định nghĩa không được trang trí 780 [cho cùng một hàm/phương thức]. Các định nghĩa được trang trí ____ 780 chỉ vì 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í 780, trong khi loại sau được sử dụng trong thời gian chạy nhưng nên bị bỏ qua bởi trình kiểm tra loại. Trong thời gian chạy, gọi trực tiếp chức năng được trang trí ____ 780 sẽ tăng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]86. Một ví dụ về quá tải cung cấp một loại chính xác hơn có thể được thể hiện bằng cách sử dụng một liên kết hoặc biến loại:
Mới trong phiên bản 3.11.
@Gõ.Overload¶typing.final¶Trình trang trí
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]80 cho phép mô tả các chức năng và phương pháp hỗ trợ nhiều kết hợp khác nhau của các loại đối số. Một loạt các định nghĩa được trang trí ____ 780 phải được theo sau bởi chính xác một định nghĩa không được trang trí 780 [cho cùng một hàm/phương thức]. Các định nghĩa được trang trí ____ 780 chỉ vì 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í 780, trong khi loại sau được sử dụng trong thời gian chạy nhưng nên bị bỏ qua bởi trình kiểm tra loại. Trong thời gian chạy, gọi trực tiếp chức năng được trang trí ____ 780 sẽ tăng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]86. Một ví dụ về quá tải cung cấp một loại chính xác hơn có thể được thể hiện bằng cách sử dụng một liên kết hoặc biến loại:
20 10 GFG21
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.PEP 591 for more details.
Đã thay đổi trong phiên bản 3.11: Các chức năng quá tải hiện có thể được nội tâm khi chạy bằng cách sử dụng def get_user_name[user_id: UserId] -> str:
...
# passes type checking
user_a = get_user_name[UserId[42351]]
# fails type checking; an int is not a UserId
user_b = get_user_name[-1]
87.
gõ.get_overloads [func] ¶The decorator will now set the
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]94 attribute to
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]43 on the decorated object. Thus, a check like
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]96 can be used at runtime to determine whether an object
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]97 has been marked as final. If the decorated object does not support setting attributes, the decorator returns the object unchanged without raising an exception. Trả về một chuỗi các định nghĩa được trang trí ____ 780 cho FUNC. Func là đối tượng chức năng để thực hiện chức năng quá tải. Ví dụ: với định nghĩa của
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]89 trong tài liệu cho
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]80,
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]91 sẽ trả về một chuỗi ba đối tượng chức năng cho ba quá tải được xác định. Nếu được gọi trên một chức năng không có quá tải,
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]87 trả về một chuỗi trống.typing.no_type_check¶
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]87 có thể được sử dụng để hướng nội một chức năng quá tải trong thời gian chạy.
gõ.clear_overloads [] ¶decorator. With a class, it applies recursively to all methods and classes defined in that class [but not to methods defined in its superclasses or subclasses].
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 để đòi lại bộ nhớ được sử dụng bởi sổ đăng ký.
@Gõ.Final¶typing.no_type_check_decorator¶Một người trang trí để chỉ ra để gõ các trình kiểm tra rằng phương pháp được trang trí không thể được ghi đè, và lớp được trang trí không thể được phân nhóm. Ví dụ:
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.typing.type_check_only¶Đã thay đổi trong phiên bản 3.11: Trình trang trí hiện sẽ đặt thuộc tính
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]94 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]]43 trên đối tượng được trang trí. Do đó, một kiểm tra như
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]96 có thể được sử dụng trong thời gian chạy để xác định xem một đối tượng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]97 có được đánh dấu là cuối cùng hay không. Nếu đối tượng được trang trí không hỗ trợ các thuộc tính thiết lập, người trang trí trả về đối tượng không thay đổi mà không gây ra ngoại lệ.
@gõ.no_type_check¶
20 10 GFG22
Trang trí để chỉ ra rằng chú thích không phải là gợi ý loại.
Điều này hoạt động như là người trang trí đẳng cấ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 xác định trong lớp đó [nhưng không phải là các phương thức được xác định trong các lớp học hoặc các lớp con].
Điều này làm thay đổi [các] hàm tại chỗ.get_type_hints[obj, globalns=None, localns=None, include_extras=False]¶@gõ.no_type_check_decorator¶
Người trang trí để cung cấp cho một người trang trí khác hiệu ứng
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]98.
Điều này kết thúc công cụ trang trí bằng một cái gì đó kết thúc chức năng được trang trí trong
def get_user_name[user_id: UserId] -> str: ... # passes type checking user_a = get_user_name[UserId[42351]] # fails type checking; an int is not a UserId user_b = get_user_name[-1]98.
20 10 GFG23
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]]32 không hoạt động với các bí danh loại nhập khẩu bao gồm các tài liệu tham khảo phía trước. Cho phép đánh giá các chú thích [PEP 563] có thể loại bỏ nhu cầu về hầu hết các tài liệu tham khảo chuyển tiếp.type aliases that include forward references. Enabling postponed evaluation of annotations [PEP 563] may remove the need for most forward references.
Đã thay đổi trong phiên bản 3.9: Đã thêm tham số # 'output' is of type 'int', not 'UserId'
output = UserId[23413] + UserId[54341]
08 như là một phần của PEP 593.Added
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]08 parameter as part of PEP 593.
Đã thay đổi trong phiên bản 3.11: Trước đây, # 'output' is of type 'int', not 'UserId'
output = UserId[23413] + UserId[54341]
13 đã được thêm vào cho các chú thích chức năng và phương thức nếu giá trị mặc định bằng GFG
23 đã được đặt. Bây giờ chú thích được trả lại không thay đổi.Previously,
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]13 was added for function and method annotations if a default value equal to
GFG23 was set. Now the annotation is returned unchanged. gõ.get_args [tp] ¶ gõ.get_origin [tp] ¶get_args[tp]¶ typing.get_origin[tp]¶
Cung cấp nội tâm 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
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]15, các chức năng này trả về
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]16 và
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]17. Nếu
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]16 là bí danh chung cho lớp tích hợp hoặc
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]29, thì nó sẽ được chuẩn hóa thành lớp ban đầu. Nếu
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]16 là một liên minh hoặc
GFG03 có trong một loại chung khác, thì thứ tự
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]17 có thể khác với thứ tự của các đối số gốc
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]23 do bộ nhớ đệm loại. Đối với các đối tượng không được hỗ trợ trả về
GFG23 và
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]25 tương ứng. Ví dụ:
20 10 GFG24
Mới trong phiên bản 3.8.
gõ.is_typeddict [tp] ¶is_typeddict[tp]¶Kiểm tra xem một loại là
GFG04.
Ví dụ:
20 10 GFG25
Mới trong phiên bản 3.10.
classtyping.forwardref¶typing.ForwardRef¶Một lớp được sử dụng để biểu diễn gõ nội bộ của các tham chiếu chuyển tiếp chuỗi. Ví dụ,
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]27 được chuyển đổi hoàn toàn thành
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]28. Lớp này không nên được người dùng khởi tạo, nhưng có thể được sử dụng bởi các công cụ hướng nội.
Ghi chú
PEP 585 Các loại chung như # 'output' is of type 'int', not 'UserId'
output = UserId[23413] + UserId[54341]
29 sẽ không được chuyển đổi hoàn toàn thành # 'output' is of type 'int', not 'UserId'
output = UserId[23413] + UserId[54341]
30 và do đó sẽ không tự động giải quyết thành # 'output' is of type 'int', not 'UserId'
output = UserId[23413] + UserId[54341]
31. generic types such as
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]29 will not be implicitly transformed into
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]30 and thus will not automatically resolve to
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]31.
Mới trong phiên bản 3.7.4.
Không thay đổi¶
gõ.type_checking¶TYPE_CHECKING¶Một hằng số đặc biệt được giả định là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]43 bởi người kiểm tra loại tĩnh của bên thứ 3. Đó 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: ...78 khi chạy. Cách sử dụng:
20 10 GFG26
Chú thích loại đầu tiên phải được đặt trong các trích dẫn, làm cho nó trở thành một tài liệu tham khảo phía trước, để ẩn tham chiếu
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]34 từ thời gian chạy của 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 các trích dẫn.
Ghi chú
Nếu
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]35 được sử dụng, các chú thích không được đánh giá theo thời gian định nghĩa chức năng. Thay vào đó, chúng được lưu trữ dưới dạng chuỗi 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. Điều này làm cho việc sử dụng các trích dẫn xung quanh chú thích không cần thiết [xem PEP 563].PEP 563].
Mới trong phiên bản 3.5.2.
Dòng thời gian phản đối của các tính năng chính
Một số tính năng nhất định trong
20 10 GFG98 không được chấp nhận và có thể bị xóa trong phiên bản Python trong tương lai. Bảng sau đây tóm tắt các khấu hao lớn cho sự thuận tiện của bạn. Điều này có thể thay đổi, và không phải tất cả các giá khấu được liệt kê.
from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]39 và from typing import NewType UserId = NewType['UserId', int] some_id = UserId[524313]48 | 3.8 | 3.13 | bpo-38291 |
20 10 GFG98 Phiên bản của các bộ sưu tập tiêu chuẩn | 3.9 | Chưa quyết định | PEP 585 |
# 'output' is of type 'int', not 'UserId' output = UserId[23413] + UserId[54341]41 | 3.11 | Chưa quyết định | gh-92332 |