Biến kiểu trong Python là gì?
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 Show
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
Để 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
DâyCác chuỗi được xác định bằng một dấu ngoặc đơn hoặc dấu ngoặc kép
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)
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
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
Toán tử trộn giữa số và chuỗi không được hỗ trợ
Tập thể dụcMụ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 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
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ụ 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 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 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ệ 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 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' 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 301 với tư cách là một lớp cơ sở định nghĩa rằng lớp 302 nhận một tham số kiểu duy nhất là 303. Điều này cũng làm cho 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 306 để 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, 311 có một tham số duy nhất, 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, 314 is not generic but implicitly inherits from 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 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 319 and also 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 đương 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 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 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 331 cho 332 mặc dù 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 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 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 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. 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à 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 example 33Sử dụng 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 347 được cho phép khi lớp 348 được mong đợi nếu và chỉ khi lớp 347 là lớp con của lớp 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ư 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 484 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 352 được coi là kiểu con của cả 353 và 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) 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 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ợ 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ợ 357đang gõ. Any ¶Loại đặc biệt cho biết loại không bị ràng buộc
Đã 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ụ 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ề 37New in version 3. 11. On older Python versions, 369 may be used to express the same concept. 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ụ 38 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 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 39This annotation is semantically equivalent to the following, albeit in a more succinct fashion 70Nói chung nếu một cái gì đó hiện đang theo mô hình của 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 374 would have 375 as the return type and not 376Other common use cases include
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. 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 357, mỗi loại có một cú pháp duy nhấtđang gõ. Tuple ¶Tuple type; 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 382Ví dụ. 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. 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. 385. Một 386 đơn giản tương đương với 387 và lần lượt là 388Không dùng nữa kể từ phiên bản 3. 9. ______4389 hiện hỗ trợ đăng ký ( 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. 393 hoặc viết tắt là 394. Sử dụng tốc ký đó được khuyến khích. Chi tiết
Đã 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 398 tương đương với 399 (hoặc 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 701 trên chú thích loại của nó chỉ vì nó là tùy chọn. Ví dụ 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 701 is appropriate, whether the argument is optional or not. For example 78Changed in version 3. 10. Optional can now be written as 399. See union type expressions . typing. Có thể gọi được ¶Callable type; 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 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 709 và lần lượt là 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ý ( 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 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 ( 730)Ví dụ: để chú thích một trình trang trí 731 cung cấp một 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 731 mong đợi một khả năng gọi được lấy trong một đối số đầu tiên là 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 79New in version 3. 10 Xem thêm
A variable annotated with 741 may accept a value of type 741. Ngược lại, một biến được chú thích bằng 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 741. Ví dụ 80Lưu ý rằng 743 là hiệp phương sai 81Thực tế là 743 là hiệp phương sai ngụ ý rằng tất cả các lớp con của 741 nên triển khai cùng chữ ký hàm tạo và chữ ký phương thức lớp như 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 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. 82 751 is equivalent to 749 which in turn is equivalent to 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. 754 now supports subscripting ( 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ụ 83 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 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ệ 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 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 764 hoặc 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 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 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 ( 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 303 can be annotated with metadata 772 via the typehint 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 773 and has no special logic for metadata 772, it should ignore it and simply treat the type as 303. Unlike the 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 303 (which can safely ignore 772) together with runtime access to 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 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 87Passing 787 to 788 lets one access the extra annotations at runtimeThe details of the syntax
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” 53Sometimes it would be convenient to use a user-defined boolean function as a type guard. Such a function should use 797 as its return type to alert static type checkers to this intentionSử dụng 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 định
Ví dụ 54If 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 378 or 804In short, the form 805, means that if 806 returns 799, then 808 narrows from 809 to 810Ghi chú 810 need not be a narrower form of 809 – it can even be a wider form. The main reason is to allow for things like narrowing 813 to 814 even though the latter is not a subtype of the former, since 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 55This class can then be used as follows 56class typing. TypeVar ¶Type variable Usage 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 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 59Type variables can be bound to concrete types, abstract types (ABCs or protocols), and even unions of types 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 11At runtime, 820 will raise 761. In general, 764 and 765 should not be used with typesType variables may be marked covariant or contravariant by passing 824 or 825. See PEP 484 for more details. By default, type variables are invariantclass typing. TypeVarTuple ¶Type variable tuple. A specialized form of 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ụ 12Note the use of the unpacking operator 827 in 828. Conceptually, you can think of 829 as a tuple of type variables 830. 828 would then become 832, which is equivalent to 833. (Note that in older versions of Python, you might see this written using 834 instead, as 835. )Type variable tuples must always be unpacked. This helps distinguish type variable tuples from normal type variables 13Type variable tuples can be used in the same contexts as normal type variables. For example, in class definitions, arguments, and return types 14Type variable tuples can be happily combined with normal type variables 15However, note that at most one type variable tuple may appear in a single list of type arguments or type parameters 16Finally, an unpacked type variable tuple can be used as the type annotation of 836 17In contrast to non-unpacked annotations of 836 - e. g. 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 - 840 enables reference to the types of the individual arguments in 836. Here, this allows us to ensure the types of the 836 passed to 843 match the types of the (positional) arguments of 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 827 on a 846 is equivalent to using 834 to mark the type variable tuple as having been unpacked 18Trên thực tế, 834 có thể được sử dụng thay thế cho 827 trong ngữ cảnh của các loại. You might see 834 being used explicitly in older versions of Python, where 827 couldn’t be used in certain places 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 852Usage 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í 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 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 709. Tuy nhiên điều này gây ra hai vấn đề
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, 869 and 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. 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 836. 870 represents the mapping of keyword parameters to their values in a given call, and should be only be used to annotate 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, 869 và 870 lần lượt là các thể hiện của 878 và 879Các biến đặc tả tham số được tạo bằng 824 hoặc 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ố 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
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 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 878, and 870 is an instance of 879. They are intended for runtime introspection and have no special meaning to static type checkersCalling 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 42New in version 3. 10 typing. AnyStr ¶ 896 is a 897 defined as 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 43class typing. Protocol(Generic) ¶Base class for protocol classes. Protocol classes are defined like this 44Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example 45See PEP 544 for more details. Protocol classes decorated with 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 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 764 and 765. This raises 761 when applied to a non-protocol class. This allows a simple-minded structural check, very similar to “one trick ponies” in 503 such as 351. For example 47Ghi chú 899 will check only the presence of the required methods, not their type signatures. For example, 506 is a class, therefore it passes an 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 509 method exists only to raise a 761 with a more informative message, therefore making it impossible to call (instantiate) 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 512Usage 48This is equivalent to 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 513 giving a dict that maps the field names to the field types. (The field names are in the 514 attribute and the default values are in the 515 attribute, both of which are part of the 516 API. ) 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 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 519 and 513 attributes are now regular dictionaries instead of instances of 521. Changed in version 3. 9. Removed the 519 attribute in favor of the more standard 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 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
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 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 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à 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 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ữ 534 hoặc 799 làm giá trị của đối số 539. 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 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 546 có ba mục. 772, 548 và 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), 555, 556 và . __total__ ¶ 558 đưa ra giá trị của đối số 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__ ¶ 560 và 561 lần lượt trả về các đối tượng 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ố 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 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 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ư 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ý ( 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 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ư 581 hoặc 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ý ( 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 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ư 586Không dùng nữa kể từ phiên bản 3. 9. ______5585 hiện hỗ trợ đăng ký ( 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 589Không dùng nữa kể từ phiên bản 3. 9. ______5589 hiện hỗ trợ đăng ký ( 357). Xem PEP 585 và Loại bí danh chung . Ghi chú 386 là một dạng đặc biệtTương ứng với các loại trong lớp đang gõ. DefaultDict(bộ sưu tập. defaultdict, MutableMapping[KT, VT]) ¶a, b = 3, 4
print(a, b)
|