Python hoàn toàn hướng đối tượng và không được "gõ tĩnh". Bạn không cần phải khai báo các biến trước khi sử dụng chúng, hoặc khai báo kiểu của chúng. Mỗi biến trong Python là một đối tượng
Hướng dẫn này sẽ đi qua một số loại biến cơ bản
Số
Python hỗ trợ hai loại số - số nguyên [số nguyên] và số dấu phẩy động [số thập phân]. [Nó cũng hỗ trợ các số phức, điều này sẽ không được giải thích trong hướng dẫn này]
Để xác định một số nguyên, sử dụng cú pháp sau
myint = 7
print[myint]
Để xác định số dấu phẩy động, bạn có thể sử dụng một trong các ký hiệu sau
myfloat = 7.0
print[myfloat]
myfloat = float[7]
print[myfloat]
Dây
Các chuỗi được xác định bằng một dấu ngoặc đơn hoặc dấu ngoặc kép
mystring = 'hello'
print[mystring]
mystring = "hello"
print[mystring]
Sự khác biệt giữa hai loại này là việc sử dụng dấu ngoặc kép giúp dễ dàng bao gồm dấu nháy đơn [trong khi những dấu nháy đơn này sẽ kết thúc chuỗi nếu sử dụng dấu nháy đơn]
mystring = "Don't worry about apostrophes"
print[mystring]
Có các biến thể bổ sung trong việc xác định chuỗi giúp dễ dàng đưa vào những thứ như dấu xuống dòng, dấu gạch chéo ngược và ký tự Unicode. Những điều này nằm ngoài phạm vi của hướng dẫn này, nhưng được đề cập trong tài liệu Python
Các toán tử đơn giản có thể được thực thi trên các số và chuỗi
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
Bài tập có thể được thực hiện trên nhiều biến "đồng thời" trên cùng một dòng như thế này
a, b = 3, 4
print[a, b]
Toán tử trộn giữa số và chuỗi không được hỗ trợ
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
Tập thể dục
Mục tiêu của bài tập này là tạo một chuỗi, một số nguyên và một số dấu phẩy động. Chuỗi phải được đặt tên là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]66 và phải chứa từ "xin chào". Số dấu phẩy động phải được đặt tên là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]67 và phải chứa số 10. 0 và số nguyên phải được đặt tên là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]68 và phải chứa số 20
Thời gian chạy Python không thực thi chức năng và chú thích loại biến. Chúng có thể được sử dụng bởi các công cụ của bên thứ ba như trình kiểm tra loại, IDE, linters, v.v.
Mô-đun này cung cấp hỗ trợ thời gian chạy cho các gợi ý loại. Hỗ trợ cơ bản nhất bao gồm các loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]27,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]29 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]]30. Để biết thông số kỹ thuật đầy đủ, vui lòng xem PEP 484. Để có phần giới thiệu đơn giản về gợi ý nhập, hãy xem PEP 483
Hàm bên dưới nhận và trả về một chuỗi và được chú thích như sau
myfloat = 7.0
print[myfloat]
myfloat = float[7]
print[myfloat]
2Trong hàm
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]31, đối số
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]32 được mong đợi là kiểu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33 và kiểu trả về là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33. Các kiểu con được chấp nhận làm đối số
Các tính năng mới thường xuyên được thêm vào mô-đun
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25. Gói typing_extensions cung cấp các bản sao của các tính năng mới này cho các phiên bản Python cũ hơn
Để biết tóm tắt về các tính năng không dùng nữa và dòng thời gian không dùng nữa, vui lòng xem Dòng thời gian ngừng dùng các tính năng chính
Xem thêm
Tài liệu tại https. //đánh máy. đọcthedocs. io/ đóng vai trò là tài liệu tham khảo hữu ích cho các tính năng của hệ thống nhập, các công cụ liên quan đến đánh máy hữu ích và các phương pháp hay nhất về đánh máy
PEP liên quan¶
Kể từ lần đầu giới thiệu các gợi ý loại trong PEP 484 và PEP 483, một số PEP đã sửa đổi và nâng cao khung của Python cho các chú thích loại. Bao gồm các
- PEP 526. Cú pháp cho chú thích biến
Giới thiệu cú pháp để chú thích các biến bên ngoài định nghĩa hàm và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
36 - PEP 544. giao thức. Phân loại cấu trúc [gõ vịt tĩnh]
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
37 và trang tríVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
38 - PEP 585. Nhập gợi ý chung trong bộ sưu tập tiêu chuẩn
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
39 và khả năng sử dụng các lớp thư viện tiêu chuẩn làm các loại chung - PEP 586. Các loại chữ
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
40 - PEP 589. đã gõDict. Nhập gợi ý cho từ điển với một bộ phím cố định
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
41 - PEP 591. Thêm một vòng loại cuối cùng để gõ
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
42 và trang tríVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
43 - PEP 593. Chức năng linh hoạt và chú thích biến
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 - PEP 604. Cho phép viết các loại công đoàn là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
45Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
46 và khả năng sử dụng toán tử nhị phân hoặc toán tửVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
47 để biểu thị một kết hợp các loại - PEP 612. Tham số Thông số kỹ thuật Biến
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 vàVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49 - PEP 613. Bí danh loại rõ ràng
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
50 - PEP 646. Generic biến thể
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
51 - PEP 647. Bảo vệ loại do người dùng xác định
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52 - PEP 655. Đánh dấu các mục TypedDict riêng lẻ là bắt buộc hoặc có khả năng bị thiếu
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 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]]
54 - PEP 673. tự loại
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
55 - PEP 675. Loại chuỗi ký tự tùy ý
Giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
56 - PEP 681. Biến đổi lớp dữ liệu
Giới thiệu trình trang trí
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
57
Nhập bí danh¶
Bí danh loại được xác định bằng cách gán loại cho bí danh. Trong ví dụ này,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]58 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]59 sẽ được coi là từ đồng nghĩa có thể hoán đổi cho nhau
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
Bí danh loại rất hữu ích để đơn giản hóa chữ ký loại phức tạp. Ví dụ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
3Lưu ý rằng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60 dưới dạng gợi ý loại là trường hợp đặc biệt và được thay thế bằng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]61
Loại mới¶
Sử dụng trình trợ giúp
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 để tạo các loại riêng biệt
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
7Trình kiểm tra kiểu tĩnh sẽ xử lý kiểu mới như thể nó là một lớp con của kiểu ban đầu. Điều này rất hữu ích trong việc giúp bắt các lỗi logic
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
8Bạn vẫn có thể thực hiện tất cả các hoạt động của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63 trên một biến loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]64, nhưng kết quả sẽ luôn là loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63. Điều này cho phép bạn vượt qua một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]64 bất cứ nơi nào có thể mong đợi một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63, nhưng sẽ ngăn bạn vô tình tạo một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]64 theo cách không hợp lệ
a, b = 3, 4
print[a, b]
5Lưu ý rằng các kiểm tra này chỉ được thực thi bởi trình kiểm tra kiểu tĩnh. Khi chạy, câu lệnh
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]69 sẽ biến
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]70 thành một hàm có thể gọi được và ngay lập tức trả về bất kỳ tham số nào bạn truyền cho nó. Điều đó có nghĩa là biểu thức
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]71 không tạo ra một lớp mới hoặc giới thiệu nhiều chi phí ngoài cuộc gọi hàm thông thường
Chính xác hơn, biểu thức
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]72 luôn đúng trong thời gian chạy
Không hợp lệ để tạo một loại phụ của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]70
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
1Tuy nhiên, có thể tạo một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 dựa trên một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 'có nguồn gốc'
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
4và đánh máy cho
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]76 sẽ hoạt động như mong đợi
Xem PEP 484 để biết thêm chi tiết
Ghi chú
Nhớ lại rằng việc sử dụng bí danh kiểu khai báo hai kiểu tương đương với nhau. Thực hiện
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]77 sẽ làm cho trình kiểm tra kiểu tĩnh coi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]78 hoàn toàn tương đương với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]79 trong mọi trường hợp. Điều này hữu ích khi bạn muốn đơn giản hóa chữ ký loại phức tạp
Ngược lại,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 tuyên bố một kiểu là kiểu con của kiểu khác. Thực hiện
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]81 sẽ khiến trình kiểm tra kiểu tĩnh coi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]70 là một phân lớp của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]79, có nghĩa là không thể sử dụng giá trị kiểu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]79 ở những nơi mong đợi giá trị kiểu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]70. Điều này hữu ích khi bạn muốn ngăn ngừa các lỗi logic với chi phí thời gian chạy tối thiểu
Mới trong phiên bản 3. 5. 2
Đã thay đổi trong phiên bản 3. 10. ______762 bây giờ là một lớp chứ không phải là một hàm. Có một số chi phí thời gian chạy bổ sung khi gọi
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 qua một chức năng thông thường. Tuy nhiên, chi phí này sẽ giảm trong 3. 11. 0.
Có thể gọi¶
Các khung mong đợi các chức năng gọi lại của các chữ ký cụ thể có thể được gợi ý loại bằng cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]88
Ví dụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]8
Có thể khai báo kiểu trả về của một hàm có thể gọi được mà không chỉ định chữ ký cuộc gọi bằng cách thay thế dấu chấm lửng bằng chữ cho danh sách các đối số trong gợi ý kiểu.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]89
Các khả năng gọi được lấy các khả năng gọi khác làm đối số có thể chỉ ra rằng các loại tham số của chúng phụ thuộc vào nhau bằng cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48. Ngoài ra, nếu khả năng gọi đó thêm hoặc xóa đối số khỏi các khả năng gọi khác, toán tử
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 có thể được sử dụng. Chúng có dạng tương ứng là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]92 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]93
Đã thay đổi trong phiên bản 3. 10. ______728 hiện hỗ trợ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49. Xem PEP 612 để biết thêm chi tiết.
Xem thêm
Tài liệu về
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 cung cấp các ví dụ về cách sử dụng trong
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28
Thuốc generic¶
Vì thông tin loại về các đối tượng được giữ trong vùng chứa không thể được suy luận tĩnh theo cách chung chung, nên các lớp cơ sở trừu tượng đã được mở rộng để hỗ trợ đăng ký biểu thị các loại dự kiến cho các phần tử vùng chứa
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]0
Generics có thể được tham số hóa bằng cách sử dụng một nhà máy có sẵn trong cách gõ có tên là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]29
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]0
Loại chung do người dùng định nghĩa¶
Một lớp do người dùng định nghĩa có thể được định nghĩa là một lớp chung
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]1
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
301 với tư cách là một lớp cơ sở định nghĩa rằng lớp one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
302 nhận một tham số kiểu duy nhất là one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303. Điều này cũng làm cho one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303 hợp lệ như một kiểu trong thân lớpLớp cơ sở
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 định nghĩa
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
306 để one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
307 có giá trị như một loạ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]]2
Một loại chung có thể có bất kỳ số lượng biến loại nào. Tất cả các loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]29 đều được phép làm tham số cho một loại chung
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]3
Mỗi đối số biến loại cho
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 phải khác biệt. Điều này là không hợp lệ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]4
Bạn có thể sử dụng đa thừa kế với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]5
Khi kế thừa từ các lớp chung, một số biến kiểu có thể được sửa
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]6
Trong trường hợp này,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
311 có một tham số duy nhất, one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303Sử dụng một lớp chung mà không chỉ định tham số loại giả sử ____726 cho mỗi vị trí. In the following example,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
314 is not generic but implicitly inherits from one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
315Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]7
Bí danh loại chung do người dùng xác định cũng được hỗ trợ. Examples
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]8
Changed in version 3. 7. ______730 không còn có siêu dữ liệu tùy chỉnh.
Generics do người dùng định nghĩa cho các biểu thức tham số cũng được hỗ trợ thông qua các biến đặc tả tham số ở dạng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
317. The behavior is consistent with type variables’ described above as parameter specification variables are treated by the typing module as a specialized type variable. Một ngoại lệ cho điều này là một danh sách các loại có thể được sử dụng để thay thế một Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]9
Furthermore, a generic with only one parameter specification variable will accept parameter lists in the forms
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
319 and also one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
320 for aesthetic reasons. 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 đươngone = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
30Xin lưu ý rằng thuốc generic có
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 có thể không đúng với
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
322 sau khi thay thế trong một số trường hợp vì chúng chủ yếu dành cho kiểm tra loại tĩnhĐã thay đổi trong phiên bản 3. 10.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 can now be parameterized over parameter expressions. See
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 and PEP 612 for more details.
A user-defined generic class can have ABCs as base classes without a metaclass conflict. Generic metaclasses are not supported. The outcome of parameterizing generics is cached, and most types in the typing module are hashable and comparable for equality
Loại Vector = list[float]
def scale[scalar: float, vector: Vector] -> Vector:
return [scalar * num for num in vector]
# passes type checking; a list of floats qualifies as a Vector.
new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26¶
Một loại đặc biệt của loại là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26. Trình kiểm tra loại tĩnh sẽ coi mọi loại là tương thích với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 là tương thích với mọi loại
This means that it is possible to perform any operation or method call on a value of type
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 and assign it to any variable
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
31Lưu ý rằng không có kiểm tra loại nào được thực hiện khi gán giá trị loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 cho loại chính xác hơn. Ví dụ: trình kiểm tra kiểu tĩnh không báo lỗi khi gán
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
331 cho one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
332 mặc dù one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
332 được khai báo là kiểu Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33 và nhận giá trị
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63 khi chạy
Hơn nữa, tất cả các hàm không có kiểu trả về hoặc kiểu tham số sẽ mặc định mặc định sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
32Hành vi này cho phép sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 làm lối thoát hiểm khi bạn cần kết hợp mã được nhập động và mã tĩnh
Contrast the behavior of
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 with the behavior of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
339. Tương tự như Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26, mỗi loại là một kiểu con của
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
339. Tuy nhiên, không giống như Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26, điều ngược lại không đúng.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
339 is not a subtype of every other typeĐiều đó có nghĩa là khi loại của một giá trị là
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
339, trình kiểm tra loại sẽ từ chối hầu hết tất cả các hoạt động trên nó và gán nó cho một biến [hoặc sử dụng nó làm giá trị trả về] của loại chuyên biệt hơn là lỗi loại. For exampleone = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
33Sử dụng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
339 để chỉ ra rằng một giá trị có thể là bất kỳ loại nào theo cách an toàn về kiểu. Sử dụng Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26 để chỉ ra rằng một giá trị được nhập động
Phân nhóm danh nghĩa và phân nhóm cấu trúc¶
Ban đầu, PEP 484 đã định nghĩa hệ thống kiểu tĩnh Python là sử dụng kiểu con danh nghĩa. Điều này có nghĩa là lớp
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
347 được cho phép khi lớp one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
348 được mong đợi nếu và chỉ khi lớp one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
347 là lớp con của lớp one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
348Yêu cầu này trước đây cũng được áp dụng cho các lớp cơ sở trừu tượng, chẳng hạn như
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
351. Vấn đề với cách tiếp cận này là một lớp phải được đánh dấu rõ ràng để hỗ trợ chúng, điều này không phổ biến và không giống như những gì người ta thường làm trong mã Python được gõ động thành ngữ. Ví dụ: điều này phù hợp với PEP 484one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
34PEP 544 cho phép giải quyết vấn đề này bằng cách cho phép người dùng viết đoạn mã trên mà không có lớp cơ sở rõ ràng trong định nghĩa lớp, cho phép
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
352 được coi là kiểu con của cả one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
353 và one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
354 bằng trình kiểm tra kiểu tĩnh. Điều này được gọi là phân nhóm cấu trúc [hoặc phân nhóm tĩnh]one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
35Hơn nữa, bằng cách phân lớp một lớp đặc biệt
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]37, người dùng có thể xác định các giao thức tùy chỉnh mới để tận hưởng đầy đủ việc phân loại cấu trúc [xem ví dụ bên dưới]
Nội dung học phần¶
The module defines the following classes, functions and decorators
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 đã tồn tại từ trước, lớp này cũng mở rộng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 để hỗ trợ các biến loại bên trong
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357. 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ợ one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357Các loại dự phòng không được dùng nữa kể từ Python 3. 9 nhưng trình thông dịch sẽ không đưa ra cảnh báo phản đối nào. Dự kiến, trình kiểm tra loại sẽ gắn cờ các loại không dùng nữa khi chương trình được kiểm tra nhắm mục tiêu Python 3. 9 hoặc mới hơn
Các loại không dùng nữa sẽ bị xóa khỏi mô-đun
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25 trong phiên bản Python đầu tiên được phát hành 5 năm sau khi phát hành Python 3. 9. 0. Xem chi tiết trong PEP 585—Type Gợi ý Generics Trong Bộ sưu tập Tiêu chuẩn
Kiểu gõ đặc biệt¶
Các loại đặc biệt¶
Chúng có thể được sử dụng làm loại trong chú thích và không hỗ trợ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357đang gõ. Any ¶Loại đặc biệt cho biết loại không bị ràng buộc
Mọi loại đều tương thích với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26 tương thích với mọi loại
Đã thay đổi trong phiên bản 3. 11. ______726 hiện có thể được sử dụng làm lớp cơ sở. Điều này có thể hữu ích để tránh các lỗi trình kiểm tra kiểu với các lớp có thể gõ ở bất cứ đâu hoặc rất năng động.
typing. Chuỗi ký tự ¶Loại đặc biệt chỉ bao gồm các chuỗi ký tự. Một chuỗi ký tự tương thích với
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]56, cũng như một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]56 khác, nhưng một đối tượng được nhập là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33 thì không. Một chuỗi được tạo bằng cách soạn các đối tượng kiểu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]56 cũng được chấp nhận là một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]56
Ví dụ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
36Điều này hữu ích cho các API nhạy cảm nơi các chuỗi tùy ý do người dùng tạo có thể gây ra sự cố. Ví dụ: hai trường hợp trên tạo ra lỗi trình kiểm tra kiểu có thể dễ bị tấn công SQL injection
Xem PEP 675 để biết thêm chi tiết
Mới trong phiên bản 3. 11
đang gõ. Không bao giờ ¶Loại dưới cùng, một loại không có thành viên
Điều này có thể được sử dụng để xác định một hàm không bao giờ được gọi hoặc một hàm không bao giờ trả về
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
37New in version 3. 11. On older Python versions,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
369 may be used to express the same concept. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
370 đã được thêm vào để làm cho ý nghĩa rõ ràng hơn. đang gõ. NoReturn ¶Special type indicating that a function never returns. Ví dụ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
38one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
369 can also be used as a bottom type, a type that has no values. Starting in Python 3. 11, thay vào đó nên sử dụng loại one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
370 cho khái niệm này. Type checkers should treat the two equivalentlyMới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 2
đang gõ. Self ¶Loại đặc biệt để đại diện cho lớp kèm theo hiện tại. For example
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
39This annotation is semantically equivalent to the following, albeit in a more succinct fashion
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
70Nói chung nếu một cái gì đó hiện đang theo mô hình của
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
71You should use
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]55 as calls to
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
374 would have one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
375 as the return type and not one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
376Other common use cases include
Các
377 được sử dụng làm hàm tạo thay thế và trả về các thể hiện của tham sốone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
378one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Chú thích một phương thức
379 trả về selfone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Xem PEP 673 để biết thêm chi tiết
Mới trong phiên bản 3. 11
typing. TypeAlias ¶Chú thích đặc biệt để khai báo rõ ràng một loại bí danh . For example.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
72Xem PEP 613 để biết thêm chi tiết về bí danh loại rõ ràng
New in version 3. 10
Special forms¶
Chúng có thể được sử dụng làm loại trong chú thích bằng cách sử dụng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357, mỗi loại có một cú pháp duy nhấtđang gõ. Tuple ¶Tuple type;
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
381 is the type of a tuple of two items with the first item of type X and the second of type Y. The type of the empty tuple can be written as one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
382Ví dụ.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
383 là một bộ gồm hai phần tử tương ứng với các biến kiểu T1 và T2. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
384 is a tuple of an int, a float and a stringĐể chỉ định một bộ có độ dài thay đổi thuộc loại đồng nhất, hãy sử dụng dấu chấm lửng bằng chữ, e. g.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
385. Một one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
386 đơn giản tương đương với one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
387 và lần lượt là one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
388Không dùng nữa kể từ phiên bản 3. 9. ______4389 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . đang gõ. Liên minh ¶loại hình công đoàn;
Để xác định một công đoàn, sử dụng e. g.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
393 hoặc viết tắt là one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
394. Sử dụng tốc ký đó được khuyến khích. Chi tiếtCác đối số phải là các loại và phải có ít nhất một
Liên minh công đoàn bị san phẳng, đ. g
73one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Liên minh của một đối số duy nhất biến mất, e. g
74one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Các đối số dư thừa bị bỏ qua, e. g
75one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Khi so sánh các công đoàn, thứ tự đối số bị bỏ qua, e. g
76one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Bạn không thể phân lớp hoặc khởi tạo một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
27Bạn không thể viết
396one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Đã thay đổi trong phiên bản 3. 7. Không xóa các lớp con rõ ràng khỏi liên kết trong thời gian chạy.
Đã thay đổi trong phiên bản 3. 10. Các hiệp hội hiện có thể được viết là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]45. Xem biểu thức kiểu liên kết . đang gõ. Tùy chọn ¶
loại tùy chọn
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
398 tương đương với one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
399 [hoặc one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
700]Lưu ý rằng đây không phải là khái niệm giống như đối số tùy chọn, là đối số có giá trị mặc định. Đối số tùy chọn với giá trị mặc định không yêu cầu từ hạn định
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
701 trên chú thích loại của nó chỉ vì nó là tùy chọn. Ví dụone = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
77On the other hand, if an explicit value of
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60 is allowed, the use of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
701 is appropriate, whether the argument is optional or not. For exampleone = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
78Changed in version 3. 10. Optional can now be written as
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
399. See union type expressions . typing. Có thể gọi được ¶Callable type;
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
705 is a function of [int] -> strCú pháp đăng ký phải luôn được sử dụng với chính xác hai giá trị. danh sách đối số và kiểu trả về. Danh sách đối số phải là một danh sách các loại hoặc dấu chấm lửng;
There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]89 [literal ellipsis] can be used to type hint a callable taking any number of arguments and returning
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
707. Một Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28 đơn giản tương đương với
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
709 và lần lượt là one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
710Các khả năng gọi được lấy các khả năng gọi khác làm đối số có thể chỉ ra rằng các loại tham số của chúng phụ thuộc vào nhau bằng cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48. Ngoài ra, nếu khả năng gọi đó thêm hoặc xóa đối số khỏi các khả năng gọi khác, toán tử
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 có thể được sử dụng. Chúng có dạng tương ứng là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]92 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]93
Deprecated since version 3. 9. ______4710 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . Đã thay đổi trong phiên bản 3. 10. ______728 hiện hỗ trợ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49. Xem PEP 612 để biết thêm chi tiết.
Xem thêm
The documentation for
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 and
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 provide examples of usage with
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28typing. Nối ¶
Used with
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28 and
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 to type annotate a higher order callable which adds, removes, or transforms parameters of another callable. Cách sử dụng có dạng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
725. Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 is currently only valid when used as the first argument to 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]]28. Tham số cuối cùng của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 phải là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48 hoặc dấu chấm lửng [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
730]Ví dụ: để chú thích một trình trang trí
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
731 cung cấp một one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
732 cho chức năng được trang trí, có thể sử dụng Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49 để chỉ ra rằng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
731 mong đợi một khả năng gọi được lấy trong một đối số đầu tiên là one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
735 và trả về một khả năng gọi được với một chữ ký kiểu khác. In this case, 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]]48 indicates that the returned callable’s parameter types are dependent on the parameter types of the callable being passed in
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
79New in version 3. 10
Xem thêm
PEP 612 – Biến thông số kỹ thuật tham số [PEP đã giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 vàVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49]Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 andVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28
A variable annotated with
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741 may accept a value of type one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741. Ngược lại, một biến được chú thích bằng one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
743 có thể chấp nhận các giá trị là chính các lớp – cụ thể, nó sẽ chấp nhận đối tượng lớp của one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741. Ví dụone = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
80Lưu ý rằng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
743 là hiệp phương saione = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
81Thực tế là
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
743 là hiệp phương sai ngụ ý rằng tất cả các lớp con của one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741 nên triển khai cùng chữ ký hàm tạo và chữ ký phương thức lớp như one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741. The type checker should flag violations of this, but should also allow constructor calls in subclasses that match the constructor calls in the indicated base class. How the type checker is required to handle this particular case may change in future revisions of PEP 484Các tham số pháp lý duy nhất cho
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
749 là các lớp, Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]26, biến loại và liên kết của bất kỳ loại nào trong số này. For example.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
82one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
751 is equivalent to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
749 which in turn is equivalent to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
753, which is the root of Python’s metaclass hierarchyMới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
754 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . đang gõ. Chữ ¶Một loại có thể được sử dụng để chỉ ra cho người kiểm tra loại rằng tham số hàm hoặc biến tương ứng có giá trị tương đương với chữ được cung cấp [hoặc một trong số nhiều chữ]. Ví dụ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
83one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
756 cannot be subclassed. Trong thời gian chạy, một giá trị tùy ý được phép làm đối số kiểu cho one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
756, nhưng trình kiểm tra kiểu có thể áp đặt các hạn chế. See PEP 586 for more details about literal typesMới trong phiên bản 3. 8
Đã thay đổi trong phiên bản 3. 9. 1.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]40 now de-duplicates parameters. So sánh bình đẳng của các đối tượng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]40 không còn phụ thuộc vào thứ tự. Các đối tượng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]40 hiện sẽ đưa ra một ngoại lệ
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
761 trong quá trình so sánh bằng nếu một trong các tham số của chúng không có thể băm . typing. ClassVar ¶Cấu trúc kiểu đặc biệt để đánh dấu các biến lớp
Như đã giới thiệu trong PEP 526, một chú thích biến được bao bọc trong ClassVar cho biết rằng một thuộc tính nhất định được dùng làm biến lớp và không được đặt trên các phiên bản của lớp đó. Cách sử dụng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
84Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]36 chỉ chấp nhận các loại và không thể đăng ký thêm
Bản thân
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]36 không phải là một lớp và không nên được sử dụng với
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
764 hoặc one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
765. Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]36 không thay đổi hành vi thời gian chạy Python, nhưng nó có thể được sử dụng bởi trình kiểm tra loại của bên thứ ba. Ví dụ: trình kiểm tra loại có thể gắn cờ mã sau đây là lỗi
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
85Mới trong phiên bản 3. 5. 3
đang gõ. Cuối cùng ¶A special typing construct to indicate to type checkers that a name cannot be re-assigned or overridden in a subclass. For example
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
86There is no runtime checking of these properties. See PEP 591 for more details
Mới trong phiên bản 3. 8
đang gõ. Bắt buộc ¶ đang gõ. NotRequired ¶Special typing constructs that mark individual keys of a
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 as either required or non-required respectively
See
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 and PEP 655 for more details
Mới trong phiên bản 3. 11
đang gõ. Chú thích ¶A type, introduced in PEP 593 [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
769], to decorate existing types with context-specific metadata [possibly multiple pieces of it, as Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]44 is variadic]. Specifically, a type
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303 can be annotated with metadata one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
772 via the typehint one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
773. Siêu dữ liệu này có thể được sử dụng cho phân tích tĩnh hoặc trong thời gian chạy. If a library [or tool] encounters a typehint one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
773 and has no special logic for metadata one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
772, it should ignore it and simply treat the type as one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303. Unlike the one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
777 functionality that currently exists in the Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25 module which completely disables typechecking annotations on a function or a class, 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]]44 type allows for both static typechecking of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303 [which can safely ignore one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
772] together with runtime access to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
772 within a specific applicationCuối cùng, trách nhiệm về cách diễn giải các chú thích [nếu có] là trách nhiệm của công cụ hoặc thư viện gặp phải loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]44. Một công cụ hoặc thư viện gặp loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]44 có thể quét qua các chú thích để xác định xem chúng có đáng quan tâm hay không [e. g. , using
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
764]Khi một công cụ hoặc thư viện không hỗ trợ chú thích hoặc gặp một chú thích không xác định, nó chỉ nên bỏ qua nó và coi loại chú thích là loại cơ bản
Tùy thuộc vào công cụ sử dụng các chú thích để quyết định xem máy khách có được phép có nhiều chú thích trên một loại hay không và cách hợp nhất các chú thích đó
Vì loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]44 cho phép bạn đặt một số chú thích cùng [hoặc khác] loại trên bất kỳ nút nào, các công cụ hoặc thư viện sử dụng các chú thích đó chịu trách nhiệm xử lý các bản sao tiềm năng. For example, if you are doing value range analysis you might allow this
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
87Passing
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
787 to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
788 lets one access the extra annotations at runtimeThe details of the syntax
Đối số đầu tiên của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 phải là loại hợp lệNhiều loại chú thích được hỗ trợ [
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 hỗ trợ các đối số biến đổi]
88one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 phải được gọi với ít nhất hai đối số [
792 không hợp lệ]one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Thứ tự của các chú thích được giữ nguyên và các vấn đề cần kiểm tra bằng
89one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Nested
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 types are flattened, with metadata ordered starting with the innermost annotation
50a, b = 3, 4 print[a, b]
Duplicated annotations are not removed
51a, b = 3, 4 print[a, b]
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
44 can be used with nested and generic aliases
52a, b = 3, 4 print[a, b]
New in version 3. 9
typing. TypeGuard ¶Special typing form used to annotate the return type of a user-defined type guard function.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]52 only accepts a single type argument. At runtime, functions marked this way should return a boolean
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]52 aims to benefit type narrowing – a technique used by static type checkers to determine a more precise type of an expression within a program’s code flow. Usually type narrowing is done by analyzing conditional code flow and applying the narrowing to a block of code. The conditional expression here is sometimes referred to as a “type guard”
a, b = 3, 4
print[a, b]
53Sometimes it would be convenient to use a user-defined boolean function as a type guard. Such a function should use
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
797 as its return type to alert static type checkers to this intentionSử dụng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
798 báo cho trình kiểm tra kiểu tĩnh rằng đối với một chức năng nhất địnhThe return value is a boolean
If the return value is
799, the type of its argument is the type insideone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
52
Ví dụ
a, b = 3, 4
print[a, b]
54If
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
801 is a class or instance method, then the type in Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]52 maps to the type of the second parameter after
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
378 or one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
804In short, the form
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
805, means that if one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
806 returns one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799, then one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
808 narrows from one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
809 to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
810Ghi chú
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
810 need not be a narrower form of one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
809 – it can even be a wider form. The main reason is to allow for things like narrowing one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
813 to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
814 even though the latter is not a subtype of the former, since one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
815 is invariant. The responsibility of writing type-safe type guards is left to the userVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 also works with type variables. See PEP 647 for more details
New in version 3. 10
Building generic types¶
These are not used in annotations. They are building blocks for creating generic types
class typing. Generic ¶Abstract base class for generic types
A generic type is typically declared by inheriting from an instantiation of this class with one or more type variables. For example, a generic mapping type might be defined as
a, b = 3, 4
print[a, b]
55This class can then be used as follows
a, b = 3, 4
print[a, b]
56class typing. TypeVar ¶Type variable
Usage
a, b = 3, 4
print[a, b]
57Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function definitions. See
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 for more information on generic types. Generic functions work as follows
a, b = 3, 4
print[a, b]
58Note that type variables can be bound, constrained, or neither, but cannot be both bound and constrained
Bound type variables and constrained type variables have different semantics in several important ways. Using a bound type variable means that 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]]29 will be solved using the most specific type possible
a, b = 3, 4
print[a, b]
59Type variables can be bound to concrete types, abstract types [ABCs or protocols], and even unions of types
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
10Using a constrained type variable, however, means that 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]]29 can only ever be solved as being exactly one of the constraints given
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
11At runtime,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
820 will raise one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
761. In general, one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
764 and one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
765 should not be used with typesType variables may be marked covariant or contravariant by passing
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
824 or one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
825. See PEP 484 for more details. By default, type variables are invariantclass typing. TypeVarTuple ¶Type variable tuple. A specialized form of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
826 that enables variadic genericsA normal type variable enables parameterization with a single type. A type variable tuple, in contrast, allows parameterization with an arbitrary number of types by acting like an arbitrary number of type variables wrapped in a tuple. Ví dụ
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
12Note the use of the unpacking operator
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
827 in one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
828. Conceptually, you can think of one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
829 as a tuple of type variables one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
830. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
828 would then become one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
832, which is equivalent to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
833. [Note that in older versions of Python, you might see this written using one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
834 instead, as one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
835. ]Type variable tuples must always be unpacked. This helps distinguish type variable tuples from normal type variables
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
13Type variable tuples can be used in the same contexts as normal type variables. For example, in class definitions, arguments, and return types
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
14Type variable tuples can be happily combined with normal type variables
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
15However, note that at most one type variable tuple may appear in a single list of type arguments or type parameters
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
16Finally, an unpacked type variable tuple can be used as the type annotation of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
836# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
17In contrast to non-unpacked annotations of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
836 - e. g. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
838, which would specify that all arguments are Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63 -
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
840 enables reference to the types of the individual arguments in one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
836. Here, this allows us to ensure the types of the one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
836 passed to one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
843 match the types of the [positional] arguments of one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
844See PEP 646 for more details on type variable tuples
Mới trong phiên bản 3. 11
typing. Unpack ¶A typing operator that conceptually marks an object as having been unpacked. For example, using the unpack operator
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
827 on a one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
846 is equivalent to using one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
834 to mark the type variable tuple as having been unpacked# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
18Trên thực tế,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
834 có thể được sử dụng thay thế cho one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
827 trong ngữ cảnh của các loại. You might see one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
834 being used explicitly in older versions of Python, where one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
827 couldn’t be used in certain places# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
19Mới trong phiên bản 3. 11
lớp đang gõ. ParamSpec[name , * , bound=None , covariant=False , contravariant=False] ¶Parameter specification variable. A specialized version of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
852Usage
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
40Parameter specification variables exist primarily for the benefit of static type checkers. Chúng được sử dụng để chuyển tiếp các loại tham số của một hàm có thể gọi này sang một hàm có thể gọi khác - một mẫu thường thấy trong các hàm và trình trang trí bậc cao hơn. They are only valid when used in
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]49, or as the first argument 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]]28, or as parameters for user-defined Generics. See
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30 for more information on generic types
Ví dụ: để thêm chức năng ghi nhật ký cơ bản, người ta có thể tạo trình trang trí
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
856 để ghi nhật ký các lệnh gọi chức năng. Biến đặc tả tham số cho trình kiểm tra kiểu biết rằng có thể gọi được chuyển vào trình trang trí và có thể gọi mới được trả về bởi nó có các tham số loại phụ thuộc lẫn nhau# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
41Nếu không có
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]48, cách đơn giản nhất để chú thích điều này trước đây là sử dụng một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]29 với ràng buộc
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
709. Tuy nhiên điều này gây ra hai vấn đềTrình kiểm tra loại không thể gõ kiểm tra chức năng
860 vìone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
836 vàone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
862 phải được gõone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
26
864 may be required in the body of theone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
856 decorator when returning theone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
860 function, or the static type checker must be told to ignore theone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
867one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
Since
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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 captures both positional and keyword parameters,
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
869 and one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
870 can be used to split 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]]48 into its components.
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
869 đại diện cho bộ tham số vị trí trong một cuộc gọi nhất định và chỉ nên được sử dụng để chú thích one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
836. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
870 represents the mapping of keyword parameters to their values in a given call, and should be only be used to annotate one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
862. Cả hai thuộc tính đều yêu cầu tham số được chú thích nằm trong phạm vi. Khi chạy, one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
869 và one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
870 lần lượt là các thể hiện của one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
878 và one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
879Các biến đặc tả tham số được tạo bằng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
824 hoặc one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
825 có thể được sử dụng để khai báo các loại chung biến thể hoặc trái ngược. Đối số one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
882 cũng được chấp nhận, tương tự như Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]29. Tuy nhiên, ngữ nghĩa thực tế của những từ khóa này vẫn chưa được quyết định
New in version 3. 10
Ghi chú
Only parameter specification variables defined in global scope can be pickled
Xem thêm
PEP 612 – Biến thông số kỹ thuật tham số [PEP đã giới thiệu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
48 vàVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
49]Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
28 andVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Arguments and keyword arguments attributes of 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]]48. The
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
869 attribute of 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]]48 is an instance of
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
878, and one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
870 is an instance of one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
879. They are intended for runtime introspection and have no special meaning to static type checkersCalling
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
894 on either of these objects will return the original Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
42New in version 3. 10
typing. AnyStr ¶one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
896 is a one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
897 defined as one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
898It is meant to be used for functions that may accept any kind of string without allowing different kinds of strings to mix. For example
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
43class typing. Protocol[Generic] ¶Base class for protocol classes. Protocol classes are defined like this
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
44Such classes are primarily used with static type checkers that recognize structural subtyping [static duck-typing], for example
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
45See PEP 544 for more details. Protocol classes decorated with
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
899 [described later] act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signaturesProtocol classes can be generic, for example
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
46Mới trong phiên bản 3. 8
@typing. runtime_checkable ¶Mark a protocol class as a runtime protocol
Such a protocol can be used with
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
764 and one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
765. This raises one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
761 when applied to a non-protocol class. This allows a simple-minded structural check, very similar to “one trick ponies” in a, b = 3, 4
print[a, b]
503 such as one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
351. For example# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
47Ghi chú
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
899 will check only the presence of the required methods, not their type signatures. For example, a, b = 3, 4
print[a, b]
506 is a class, therefore it passes an one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
765 check against Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]28. However, the
a, b = 3, 4
print[a, b]
509 method exists only to raise a one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
761 with a more informative message, therefore making it impossible to call [instantiate] a, b = 3, 4
print[a, b]
506Mới trong phiên bản 3. 8
Other special directives¶
These are not used in annotations. They are building blocks for declaring types
class typing. NamedTuple ¶Typed version of
a, b = 3, 4
print[a, b]
512Usage
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
48This is equivalent to
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
49To give a field a default value, you can assign to it in the class body
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]80
Các trường có giá trị mặc định phải xuất hiện sau bất kỳ trường nào không có giá trị mặc định
The resulting class has an extra attribute
a, b = 3, 4
print[a, b]
513 giving a dict that maps the field names to the field types. [The field names are in the a, b = 3, 4
print[a, b]
514 attribute and the default values are in the a, b = 3, 4
print[a, b]
515 attribute, both of which are part of the a, b = 3, 4
print[a, b]
516 API. ]a, b = 3, 4
print[a, b]
517 subclasses can also have docstrings and methodsVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]81
a, b = 3, 4
print[a, b]
517 subclasses can be genericVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Backward-compatible 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]]83
Changed in version 3. 6. Added support for PEP 526 variable annotation syntax.
Changed in version 3. 6. 1. Added support for default values, methods, and docstrings.
Changed in version 3. 8. The
a, b = 3, 4
print[a, b]
519 and a, b = 3, 4
print[a, b]
513 attributes are now regular dictionaries instead of instances of a, b = 3, 4
print[a, b]
521. Changed in version 3. 9. Removed the
a, b = 3, 4
print[a, b]
519 attribute in favor of the more standard a, b = 3, 4
print[a, b]
513 attribute which has the same information. Changed in version 3. 11. Added support for generic namedtuples.
class typing. NewType[name , tp] ¶A helper class to indicate a distinct type to a typechecker, see NewType . Khi chạy, nó trả về một đối tượng trả về đối số của nó khi được gọi. 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]]84
Mới trong phiên bản 3. 5. 2
Changed in version 3. 10.
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]62 is now a class rather than a function. lớp đang gõ. TypedDict[dict] ¶
Cấu trúc đặc biệt để thêm gợi ý loại vào từ điển. Khi chạy nó là một
a, b = 3, 4
print[a, b]
525 đơn giảnVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 khai báo một loại từ điển yêu cầu tất cả các phiên bản của nó có một bộ khóa nhất định, trong đó mỗi khóa được liên kết với một giá trị của một loại nhất quán. Kỳ vọng này không được kiểm tra trong thời gian chạy mà chỉ được thực thi bởi trình kiểm tra loại. Cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]85
Để cho phép sử dụng tính năng này với các phiên bản Python cũ hơn không hỗ trợ PEP 526,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 hỗ trợ thêm hai dạng cú pháp tương đương
Sử dụng một chữ
525 làm đối số thứ haia, b = 3, 4 print[a, b]
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
86Sử dụng đối số từ khóa
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]
87
Không dùng nữa kể từ phiên bản 3. 11, sẽ bị xóa trong phiên bản 3. 13. Cú pháp đối số từ khóa không được dùng trong 3. 11 và sẽ bị xóa trong 3. 13. Nó cũng có thể không được hỗ trợ bởi bộ kiểm tra kiểu tĩnh.
Cú pháp chức năng cũng nên được sử dụng khi bất kỳ khóa nào không có mã định danh hợp lệ, chẳng hạn như vì chúng là từ khóa hoặc chứa dấu gạch nối. 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]]88
Theo mặc định, tất cả các khóa phải có trong một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41. Có thể đánh dấu các khóa riêng lẻ là không bắt buộc bằng cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]54
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]89
Điều này có nghĩa là một
a, b = 3, 4
print[a, b]
531 Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 có thể bỏ qua khóa
a, b = 3, 4
print[a, b]
533Cũng có thể đánh dấu tất cả các khóa là không bắt buộc theo mặc định bằng cách chỉ định tổng số là
a, b = 3, 4
print[a, b]
534Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num 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
Điều này có nghĩa là một
a, b = 3, 4
print[a, b]
531 Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 có thể bỏ qua bất kỳ phím nào. Trình kiểm tra loại chỉ được mong đợi để hỗ trợ một chữ
a, b = 3, 4
print[a, b]
534 hoặc one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 làm giá trị của đối số a, b = 3, 4
print[a, b]
539. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 là mặc định và làm cho tất cả các mục được xác định trong nội dung lớp bắt buộcCác khóa riêng lẻ của
a, b = 3, 4
print[a, b]
541 Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 có thể được đánh dấu theo yêu cầu bằng cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]53
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]01
Loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 có thể kế thừa từ một hoặc nhiều loại
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 khác bằng cách sử dụng cú pháp dựa trên lớp. Cách sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]02
a, b = 3, 4
print[a, b]
546 có ba mục. one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
772, a, b = 3, 4
print[a, b]
548 và a, b = 3, 4
print[a, b]
549. Nó tương đương với định nghĩa nàyVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]03
Một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 không thể kế thừa từ một lớp không phải ____741, ngoại trừ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]30. Ví dụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]04
Một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 có thể là chung chung
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]05
Có thể xem xét nội quan một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 thông qua các lệnh chú thích [xem Các phương pháp hay nhất về chú thích để biết thêm thông tin về các phương pháp hay nhất về chú thích],
a, b = 3, 4
print[a, b]
555, a, b = 3, 4
print[a, b]
556 và . __total__ ¶a, b = 3, 4
print[a, b]
558 đưa ra giá trị của đối số a, b = 3, 4
print[a, b]
539. Ví dụVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]06__required_keys__ ¶
New in version 3. 9
__tùy chọn_keys__ ¶a, b = 3, 4
print[a, b]
560 và a, b = 3, 4
print[a, b]
561 lần lượt trả về các đối tượng a, b = 3, 4
print[a, b]
562 chứa các khóa bắt buộc và không bắt buộcCác khóa được đánh dấu bằng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]53 sẽ luôn xuất hiện trong ________ 5556 và các khóa được đánh dấu bằng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]54 sẽ luôn xuất hiện trong ________ 5557
Để tương thích ngược với Python 3. 10 trở xuống, cũng có thể sử dụng tính kế thừa để khai báo cả khóa bắt buộc và khóa không bắt buộc trong cùng một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41. Điều này được thực hiện bằng cách khai báo một
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 với một giá trị cho đối số
a, b = 3, 4
print[a, b]
539 và sau đó kế thừa từ nó trong một Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 khác với một giá trị khác cho
a, b = 3, 4
print[a, b]
539Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]07
New in version 3. 9
Xem PEP 589 để biết thêm ví dụ và quy tắc chi tiết về việc sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41
Mới trong phiên bản 3. 8
Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ để đánh dấu các khóa riêng lẻ là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]53 hoặc
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]54. Xem PEP 655.
Đã thay đổi trong phiên bản 3. 11. Đã thêm hỗ trợ cho các
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 chung.
Bộ sưu tập bê tông chung¶
Tương ứng với các loại tích hợp¶
lớp đang gõ. Dict[dict, MutableMapping[KT, VT]]¶A generic version of
a, b = 3, 4
print[a, b]
525. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như a, b = 3, 4
print[a, b]
577Loại này có thể được sử dụng như sau
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]08
Không dùng nữa kể từ phiên bản 3. 9. ______5578 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Danh sách[danh sách, MutableSequence[T]]¶Phiên bản chung của
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
815. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như a, b = 3, 4
print[a, b]
581 hoặc one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
351Loại này có thể được sử dụng như sau
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]09
Không dùng nữa kể từ phiên bản 3. 9. ______5583 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Set[set, MutableSet[T]]¶Một phiên bản chung của
a, b = 3, 4
print[a, b]
585. Hữu ích cho việc chú thích các loại trả lại. Để chú thích các đối số, nên sử dụng loại bộ sưu tập trừu tượng, chẳng hạn như a, b = 3, 4
print[a, b]
586Không dùng nữa kể từ phiên bản 3. 9. ______5585 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. FrozenSet[frozenset, AbstractSet[T_co]]¶Một phiên bản chung của
a, b = 3, 4
print[a, b]
589Không dùng nữa kể từ phiên bản 3. 9. ______5589 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . Ghi chú
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
386 là một dạng đặc biệtTương ứng với các loại trong a, b = 3, 4
print[a, b]
593¶
lớp đang gõ. DefaultDict[bộ sưu tập. defaultdict, MutableMapping[KT, VT]] ¶a, b = 3, 4
print[a, b]
Một phiên bản chung của
a, b = 3, 4
print[a, b]
594Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 9. ______5594 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. OrderedDict[bộ sưu tập. OrderedDict, MutableMapping[KT, VT]] ¶Một phiên bản chung của
a, b = 3, 4
print[a, b]
597Mới trong phiên bản 3. 7. 2
Không dùng nữa kể từ phiên bản 3. 9. ______5597 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Bản đồ chuỗi[bộ sưu tập. ChainMap, MutableMapping[KT, VT]] ¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
100Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. ______6100 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Bộ đếm[bộ sưu tập. Bộ đếm, Dict[T, int]] ¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
103Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. ______6103 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Deque[deque, MutableSequence[T]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
106Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 1
Không dùng nữa kể từ phiên bản 3. 9. ______6106 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . Các loại bê tông khác¶
lớp đang gõ. IO ¶ lớp gõ. TextIO ¶ lớp gõ. Nhị phânIO ¶Loại chung
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
109 và các lớp con của nó # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
110 và # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
111 đại diện cho các loại luồng I/O, chẳng hạn như được trả về bởi # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
112Không dùng nữa kể từ phiên bản 3. 8, sẽ bị xóa trong phiên bản 3. 13. Không gian tên
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
113 không được dùng nữa và sẽ bị xóa. Thay vào đó, các loại này nên được nhập trực tiếp từ Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25. lớp đang gõ. Mẫu ¶ lớp gõ. Trận đấu ¶
Các bí danh loại này tương ứng với các loại trả về từ
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
115 và # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
116. Các loại này [và các chức năng tương ứng] là chung chung trong ________ 4896 và có thể được làm cụ thể bằng cách viết _______ 6118, ________ 6119, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
120 hoặc # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
121Không dùng nữa kể từ phiên bản 3. 8, sẽ bị xóa trong phiên bản 3. 13. Không gian tên
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
122 không được dùng nữa và sẽ bị xóa. Thay vào đó, các loại này nên được nhập trực tiếp từ Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25.
Không dùng nữa kể từ phiên bản 3. 9. Các lớp
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
124 và # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
125 từ # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
126 hiện hỗ trợ one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Văn bản ¶# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
128 là bí danh của Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33. Nó được cung cấp để cung cấp đường dẫn tương thích chuyển tiếp cho mã Python 2. trong Python 2,
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
128 là bí danh của # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
131Sử dụng
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
128 để cho biết rằng một giá trị phải chứa chuỗi unicode theo cách tương thích với cả Python 2 và Python 3Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]00
Mới trong phiên bản 3. 5. 2
Không dùng nữa kể từ phiên bản 3. 11. Python 2 không còn được hỗ trợ và hầu hết các trình kiểm tra kiểu cũng không còn hỗ trợ kiểm tra kiểu mã Python 2. Việc xóa bí danh hiện chưa được lên kế hoạch, nhưng người dùng được khuyến khích sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33 thay vì
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
128 bất cứ khi nào có thể. Các lớp cơ sở trừu tượng¶
Tương ứng với các bộ sưu tập trong a, b = 3, 4
print[a, b]
503¶
lớp đang gõ. Bộ trừu tượng[Bộ sưu tập[T_co]]¶a, b = 3, 4
print[a, b]
Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
136Không dùng nữa kể từ phiên bản 3. 9. ______6136 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. ByteString[Sequence[int]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
139Loại này đại diện cho các loại
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
140, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
141 và # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
142 của chuỗi byteLà cách viết tắt của loại này, có thể sử dụng
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
140 để chú thích các đối số thuộc bất kỳ loại nào được đề cập ở trênKhông dùng nữa kể từ phiên bản 3. 9. ______6139 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Bộ sưu tập[Có kích thước, Có thể lặp lại[T_co], Container[T_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
146Mới trong phiên bản 3. 6. 0
Không dùng nữa kể từ phiên bản 3. 9. ______6146 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Vùng chứa[Chung[T_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
149Không dùng nữa kể từ phiên bản 3. 9. ______6149 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. ItemsView[MappingView, AbstractSet[tuple[KT_co, VT_co]]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
152Không dùng nữa kể từ phiên bản 3. 9. ______6152 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. KeysView[MappingView, AbstractSet[KT_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
155Không dùng nữa kể từ phiên bản 3. 9. ______6155 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Ánh xạ[Bộ sưu tập[KT], Chung[KT, VT_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
158. Loại này có thể được sử dụng như sauVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]01
Không dùng nữa kể từ phiên bản 3. 9. ______6158 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Chế độ xem ánh xạ[Sized] ¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
161Không dùng nữa kể từ phiên bản 3. 9. ______6161 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Mapping có thể thay đổi[Mapping[KT, VT]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
164Không dùng nữa kể từ phiên bản 3. 9. ______6164 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. MutableSequence[Sequence[T]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
167Không dùng nữa kể từ phiên bản 3. 9. ______6167 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. MutableSet[AbstractSet[T]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
170Không dùng nữa kể từ phiên bản 3. 9. ______6170 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
173Không dùng nữa kể từ phiên bản 3. 9. ______6173 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Chế độ xem giá trị[Chế độ xem bản đồ, Bộ sưu tập[_VT_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
176Không dùng nữa kể từ phiên bản 3. 9. ______6176 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . Tương ứng với các loại khác trong a, b = 3, 4
print[a, b]
503¶
lớp đang gõ. Có thể lặp lại[Chung[T_co]]¶a, b = 3, 4
print[a, b]
Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
180Không dùng nữa kể từ phiên bản 3. 9. ______6180 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Iterator[Iterable[T_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
183Không dùng nữa kể từ phiên bản 3. 9. ______6183 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Trình tạo[Iterator[T_co], Chung[T_co, T_contra, V_co]]¶Trình tạo có thể được chú thích theo loại chung
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
186. Ví dụVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]02
Lưu ý rằng không giống như nhiều thuốc generic khác trong mô-đun đánh máy,
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
187 của # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
188 hoạt động trái ngược, không đồng biến hoặc bất biếnNếu trình tạo của bạn chỉ mang lại giá trị, hãy đặt
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
187 và one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
707 thành Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]03
Ngoài ra, hãy chú thích trình tạo của bạn có kiểu trả về là
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
192 hoặc # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
193Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]04
Không dùng nữa kể từ phiên bản 3. 9. ______6194 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . lớp đang gõ. Có thể băm ¶Bí danh của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
196lớp đang gõ. Có thể đảo ngược[Có thể lặp lại[T_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
197Không dùng nữa kể từ phiên bản 3. 9. ______6197 hiện hỗ trợ đăng ký [
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. Xem PEP 585 và Loại bí danh chung . class typing. Sized ¶An alias to
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
400Asynchronous programming¶
class typing. Coroutine[Awaitable[V_co], Chung[T_co, T_contra, V_co]]¶Một phiên bản chung của
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
401. The variance and order of type variables correspond to those of # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
188, for exampleVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]05
Mới trong phiên bản 3. 5. 3
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
401 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . class typing. AsyncGenerator[AsyncIterator[T_co], Generic[T_co, T_contra]] ¶An async generator can be annotated by the generic type
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
405. For exampleVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]06
Unlike normal generators, async generators cannot return a value, so there is no
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
707 type parameter. As with # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
188, the # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
187 behaves contravariantlyIf your generator will only yield values, set the
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
187 to Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]07
Alternatively, annotate your generator as having a return type of either
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
411 or # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
412Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]08
Mới trong phiên bản 3. 6. 1
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
413 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . class typing. AsyncIterable[Generic[T_co]] ¶A generic version of
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
415Mới trong phiên bản 3. 5. 2
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
415 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . class typing. AsyncIterator[AsyncIterable[T_co]] ¶A generic version of
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
418Mới trong phiên bản 3. 5. 2
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
418 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . class typing. Awaitable[Generic[T_co]] ¶A generic version of
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
421Mới trong phiên bản 3. 5. 2
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
421 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . Context manager types¶
class typing. ContextManager[Generic[T_co]] ¶A generic version of
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
424Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 0
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
424 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . class typing. AsyncContextManager[Generic[T_co]] ¶A generic version of
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
427Mới trong phiên bản 3. 5. 4
Mới trong phiên bản 3. 6. 2
Deprecated since version 3. 9.
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
427 now supports subscripting [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
357]. See PEP 585 and Generic Alias Type . Protocols¶
These protocols are decorated with
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
899lớp đang gõ. SupportsAbs ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
431 that is covariant in its return typeclass typing. SupportsBytes ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
432class typing. SupportsComplex ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
433class typing. SupportsFloat ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
434class typing. SupportsIndex ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
435Mới trong phiên bản 3. 8
class typing. SupportsInt ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
436class typing. SupportsRound ¶An ABC with one abstract method
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
437 that is covariant in its return typeFunctions and decorators¶
typing. cast[typ , val] ¶Cast a value to a type
This returns the value unchanged. To the type checker this signals that the return value has the designated type, but at runtime we intentionally don’t check anything [we want this to be as fast as possible]
typing. assert_type[val , typ , /] ¶Ask a static type checker to confirm that val has an inferred type of typ
When the type checker encounters a call to
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
438, it emits an error if the value is not of the specified typeVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]09
At runtime this returns the first argument unchanged with no side effects
This function is useful for ensuring the type checker’s understanding of a script is in line with the developer’s intentions
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]10
Mới trong phiên bản 3. 11
typing. assert_never[arg , /] ¶Ask a static type checker to confirm that a line of code is unreachable
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]]11
Here, the annotations allow the type checker to infer that the last case can never execute, because
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
808 is either an Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]63 or a
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]33, and both options are covered by earlier cases. If a type checker finds that a call to
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
442 is reachable, it will emit an error. For example, if the type annotation for one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
808 was instead # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
444, the type checker would emit an error pointing out that # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
445 is of type # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
446. For a call to # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
447 to pass type checking, the inferred type of the argument passed in must be the bottom type, one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
370, and nothing elseAt runtime, this throws an exception when called
Xem thêm
Unreachable Code and Exhaustiveness Checking has more information about exhaustiveness checking with static typing
Mới trong phiên bản 3. 11
typing. reveal_type[obj , /] ¶Reveal the inferred static type of an expression
When a static type checker encounters a call to this function, it emits a diagnostic with the type of the argument. For example
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]12
This can be useful when you want to debug how your type checker handles a particular piece of code
The function returns its argument unchanged, which allows using it within an expression
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]13
Most type checkers support
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
449 anywhere, even if the name is not imported from Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25. Importing the name from
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25 allows your code to run without runtime errors and communicates intent more clearly
At runtime, this function prints the runtime type of its argument to stderr and returns it unchanged
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]14
Mới trong phiên bản 3. 11
@đang gõ. dataclass_transform ¶# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
452 may be used to decorate a class, metaclass, or a function that is itself a decorator. The presence of # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
453 tells a static type checker that the decorated object performs runtime “magic” that transforms a class, giving it # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
454-like behaviorsExample usage with a decorator function
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]15
On a base class
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]16
On a metaclass
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]17
The
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
455 classes defined above will be treated by type checkers similarly to classes created with # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
456. For example, type checkers will assume these classes have # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
457 methods that accept # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
458 and Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]32
The decorated class, metaclass, or function may accept the following bool arguments which type checkers will assume have the same effect as they would have on the
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
456 decorator. # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
461, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
462, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
463, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
464, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
465, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
466, # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
467, and # This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
468. It must be possible for the value of these arguments [one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 or a, b = 3, 4
print[a, b]
534] to be statically evaluatedThe arguments to the
# This will not work!
one = 1
two = 2
hello = "hello"
print[one + two + hello]
452 decorator can be used to customize the default behaviors of the decorated class, metaclass, or function
472 indicates whether the# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
462 parameter is assumed to be# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
799 orone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
534 if it is omitted by the callera, b = 3, 4 print[a, b]
476 indicates whether the# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
463 parameter is assumed to be True or False if it is omitted by the caller# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
478 indicates whether the# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
467 parameter is assumed to be True or False if it is omitted by the caller# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
480 specifies a static list of supported classes or functions that describe fields, similar to# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
481# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
Arbitrary other keyword arguments are accepted in order to allow for possible future extensions
Type checkers recognize the following optional arguments on field specifiers
461 indicates whether the field should be included in the synthesized# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
457 method. If unspecified,# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
461 defaults to# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
799one = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
486 provides the default value for the field# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
487 provides a runtime callback that returns the default value for the field. If neither# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
486 nor# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
487 are specified, the field is assumed to have no default value and must be provided a value when the class is instantiated# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
490 is an alias for# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
487# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
467 indicates whether the field should be marked as keyword-only. If# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
799, the field will be keyword-only. Ifone = 1 two = 2 three = one + two print[three] hello = "hello" world = "world" helloworld = hello + " " + world print[helloworld]
534, it will not be keyword-only. If unspecified, the value of thea, b = 3, 4 print[a, b]
467 parameter on the object decorated with# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
452 will be used, or if that is unspecified, the value of# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
478 on# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
452 will be used# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
499 provides an alternative name for the field. This alternative name is used in the synthesized# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
457 method# This will not work! one = 1 two = 2 hello = "hello" print[one + two + hello]
At runtime, this decorator records its arguments in the
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]801 attribute on the decorated object. It has no other runtime effect
See PEP 681 for more details
Mới trong phiên bản 3. 11
@typing. overload ¶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]]802 decorator allows describing functions and methods that support multiple different combinations of argument types. A series of
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated definitions must be followed by exactly one non-
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated definition [for the same function/method]. The
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated definitions are for the benefit of the type checker only, since they will be overwritten by the non-
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated definition, while the latter is used at runtime but should be ignored by a type checker. At runtime, calling a
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated function directly will raise
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]808. An example of overload that gives a more precise type than can be expressed using a union or a type variable
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]18
See PEP 484 for more details and comparison with other typing semantics
Changed in version 3. 11. Overloaded functions can now be introspected at runtime using
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]809. typing. get_overloads[func] ¶
Return a sequence of
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802-decorated definitions for func. func is the function object for the implementation of the overloaded function. For example, given the definition of
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]811 in the documentation for
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]802,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]813 will return a sequence of three function objects for the three defined overloads. If called on a function with no overloads,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]809 returns an empty sequence
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]809 can be used for introspecting an overloaded function at runtime
Mới trong phiên bản 3. 11
typing. clear_overloads[] ¶Clear all registered overloads in the internal registry. This can be used to reclaim the memory used by the registry
Mới trong phiên bản 3. 11
@typing. cuối cùng ¶A decorator to indicate to type checkers that the decorated method cannot be overridden, and the decorated class cannot be subclassed. For example
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]19
There is no runtime checking of these properties. See PEP 591 for more details
Mới trong phiên bản 3. 8
Changed in version 3. 11. The decorator will now set the
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]816 attribute to
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 on the decorated object. Thus, a check like Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]818 can be used at runtime to determine whether an object
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]819 has been marked as final. If the decorated object does not support setting attributes, the decorator returns the object unchanged without raising an exception. @typing. no_type_check ¶
Decorator to indicate that annotations are not type hints
This works as class or function decorator . With a class, it applies recursively to all methods and classes defined in that class [but not to methods defined in its superclasses or subclasses].
This mutates the function[s] in place
@typing. no_type_check_decorator ¶Decorator to give another decorator the
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]820 effect
This wraps the decorator with something that wraps the decorated function in
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]820@đang gõ. type_check_only ¶
Trình trang trí để đánh dấu một lớp hoặc chức năng không khả dụng khi chạy
Bản thân trình trang trí này không có sẵn trong thời gian chạy. Nó chủ yếu nhằm đánh dấu các lớp được định nghĩa trong các tệp sơ khai kiểu nếu việc triển khai trả về một thể hiện của một lớp riêng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]20
Lưu ý rằng không nên trả lại các phiên bản của các lớp riêng tư. Tốt nhất là công khai các lớp như vậy
Người trợ giúp nội tâm¶
đang gõ. get_type_hints[obj , globalns=None, localns=None, include_extras=False]¶Trả về một từ điển chứa các gợi ý kiểu cho một hàm, phương thức, mô-đun hoặc đối tượng lớp
Điều này thường giống như
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]822. Ngoài ra, các tham chiếu chuyển tiếp được mã hóa dưới dạng chuỗi ký tự được xử lý bằng cách đánh giá chúng trong các không gian tên
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]823 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]]824. Đối với một lớp
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
741, trả về một từ điển được xây dựng bằng cách hợp nhất tất cả các a, b = 3, 4
print[a, b]
513 dọc theo Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]827 theo thứ tự ngược lại
Hàm thay thế đệ quy tất cả
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]828 bằng
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
303, trừ khi Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]830 được đặt thành
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 [xem Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]44 để biết thêm thông tin]. Ví dụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]21
Ghi chú
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
788 không hoạt động với loại bí danh đã nhập bao gồm tham chiếu chuyển tiếp. Cho phép đánh giá chú thích bị trì hoãn [PEP 563] có thể loại bỏ nhu cầu đối với hầu hết các tham chiếu chuyển tiếp. Đã thay đổi trong phiên bản 3. 9. Đã thêm tham số
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]830 như một phần của PEP 593.
Đã thay đổi trong phiên bản 3. 11. Trước đây,
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]835 đã được thêm vào cho các chú thích hàm và phương thức nếu giá trị mặc định bằng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60 được đặt. Bây giờ chú thích được trả lại không thay đổi. đang gõ. get_args[tp] ¶ . typing.get_origin[tp] ¶
Cung cấp nội quan cơ bản cho các loại chung và các hình thức gõ đặc biệt
Đối với một đối tượng gõ có dạng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]837, các hàm này trả về
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]838 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]]839. 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]]838 là bí danh chung cho lớp dựng sẵn hoặc lớp
a, b = 3, 4
print[a, b]
593, thì nó sẽ được chuẩn hóa thành lớp ban đầu. Nếu Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]838 là một liên kết hoặc
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]40 được chứa trong một loại chung khác, thứ tự của
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]839 có thể khác với thứ tự của các đối số ban đầu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]845 do bộ nhớ đệm loại. Đối với các đối tượng không được hỗ trợ, hãy trả về
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]60 và
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]847 tương ứng. ví dụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]22
Mới trong phiên bản 3. 8
đang gõ. được đánh máy[đến] ¶Kiểm tra xem một loại có phải là
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]41 không
Ví dụ
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]23
New in version 3. 10
lớp đang gõ. ForwardRef ¶Một lớp được sử dụng để biểu diễn kiểu gõ bên trong của các tham chiếu chuyển tiếp chuỗi. Ví dụ:
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]849 được chuyển ngầm thành
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]850. Lớp này không nên được khởi tạo bởi người dùng, nhưng có thể được sử dụng bởi các công cụ hướng nội
Ghi chú
Các loại chung của PEP 585 chẳng hạn như
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]851 sẽ không được chuyển đổi hoàn toàn thành
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]852 và do đó sẽ không tự động phân giải thành
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]853
Mới trong phiên bản 3. 7. 4
Hằng số¶
đang gõ. TYPE_CHECKING ¶Một hằng số đặc biệt được giả định là
one = 1
two = 2
three = one + two
print[three]
hello = "hello"
world = "world"
helloworld = hello + " " + world
print[helloworld]
799 bởi trình kiểm tra loại tĩnh của bên thứ 3. Đó là a, b = 3, 4
print[a, b]
534 trong thời gian chạy. Cách sử dụngVector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]24
Chú thích loại đầu tiên phải được đặt trong dấu ngoặc kép, làm cho nó trở thành "tham chiếu chuyển tiếp", để ẩn tham chiếu
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]856 khỏi thời gian chạy trình thông dịch. Loại chú thích cho các biến cục bộ không được đánh giá, vì vậy chú thích thứ hai không cần phải được đặt trong dấu ngoặc kép
Ghi chú
Nếu sử dụng
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]857, các chú thích sẽ không được đánh giá tại thời điểm định nghĩa hàm. Thay vào đó, chúng được lưu trữ dưới dạng chuỗi trong
a, b = 3, 4
print[a, b]
513. Điều này làm cho việc sử dụng dấu ngoặc kép xung quanh chú thích là không cần thiết [xem PEP 563]Mới trong phiên bản 3. 5. 2
Dòng thời gian ngừng sử dụng các tính năng chính¶
Một số tính năng trong
Vector = list[float] def scale[scalar: float, vector: Vector] -> Vector: return [scalar * num for num in vector] # passes type checking; a list of floats qualifies as a Vector. new_vector = scale[2.0, [1.0, -4.2, 5.4]]25 không được dùng nữa và có thể bị xóa trong phiên bản tương lai của Python. Bảng sau đây tóm tắt các loại bỏ chính để thuận tiện cho bạn. Điều này có thể thay đổi và không phải tất cả các phản đối đều được liệt kê