PEP 484, cung cấp thông số kỹ thuật về giao diện của một hệ thống kiểu trong Python3, đã giới thiệu khái niệm về gợi ý kiểu. Hơn nữa, để hiểu rõ hơn về triết lý thiết kế gợi ý kiểu, điều quan trọng là phải đọc PEP 483, điều này sẽ hữu ích để hỗ trợ một pythoneer hiểu lý do tại sao Python giới thiệu một hệ thống kiểu. Mục tiêu chính của bảng gian lận này là hiển thị một số cách sử dụng phổ biến về gợi ý loại trong Python3
Mục lục
Đánh máy
Không kiểm tra loại
Với loại kiểm tra
các loại cơ bản
Chức năng
Các lớp học
Máy phát điện
Máy phát điện không đồng bộ
Trình quản lý bối cảnh
Trình quản lý bối cảnh không đồng bộ
Tránh truy cập
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
2Đối số chỉ vị trí
Nhiều giá trị trả về
Liên minh [Bất kỳ, Không] == Tùy chọn [Bất kỳ]
Hãy cẩn thận với
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
3Cẩn thận khi đúc
Chuyển tiếp tài liệu tham khảo
Hoãn đánh giá các chú thích
Nhập bí danh
Định nghĩa một
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
4Sử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 làm mẫuSử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 vàfrom typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
7 làm mẫu lớpQuy tắc phạm vi cho
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5Hạn chế trong một tập hợp cố định các loại có thể
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 với giới hạn trên@overload
Tập tin sơ khai
Không kiểm tra loại¶
________số 8
đầu ra
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer
Với kiểm tra loại¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]0
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]1
Các loại cơ bản¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]2
Chức năng¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]3
Các lớp học¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
Máy phát điện¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]5
Máy phát điện không đồng bộ¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]6
Trình quản lý ngữ cảnh¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]7
Trình quản lý bối cảnh không đồng bộ¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer0
Tránh truy cập from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
2¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer1
đầu ra
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer2
Đối số chỉ vị trí¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer3
đầu ra
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer4
Nhiều giá trị trả về¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer5
Hợp nhất [Bất kỳ, Không] == Tùy chọn [Bất kỳ]¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer6
Hãy cẩn thận với from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
3¶
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer7
đầu ra
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer8
tuyên bố rõ ràng
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer9
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]00
Cẩn thận khi đúc¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]01
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]02
Chuyển tiếp tài liệu tham khảo¶
Dựa trên PEP 484, nếu chúng tôi muốn tham chiếu một loại trước khi nó được khai báo, chúng tôi phải sử dụng chuỗi ký tự để ngụ ý rằng có một loại tên đó sau này trong tệp
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]03
Ghi chú
Có một số vấn đề mà mypy không phàn nàn Forward References. Nhận thêm thông tin từ Số phát hành #948
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]04
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]05
Hoãn đánh giá các chú thích¶
Mới trong Python 3. 7
PEP 563 - Hoãn Đánh giá Chú thích
Trước Python 3. 7
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]06
Sau Python 3. 7 [bao gồm 3. 7]
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]07
Ghi chú
Chú thích chỉ có thể được sử dụng trong phạm vi tên đã tồn tại. Do đó, tham chiếu chuyển tiếp không hỗ trợ trường hợp tên không có sẵn trong phạm vi hiện tại. Đánh giá chú thích bị trì hoãn sẽ trở thành hành vi mặc định trong Python 4. 0
Nhập bí danh¶
Giống như
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]52 hoặc
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]53 trong c/C++
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]08
Bí danh loại được xác định bởi các phép gán biến đơn giản
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]09
Định nghĩa một from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
4¶
Không giống như bí danh,
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]4 trả về một loại riêng biệt nhưng giống hệt với loại ban đầu khi chạy
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]10
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]11
đọc thêm
Vấn đề #1284
Sử dụng from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
5 làm mẫu¶
Giống như c ++
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]57
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]12
Python sử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]5
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]13
Sử dụng from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
5 và from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
7 làm mẫu lớp¶
Giống như c ++
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]61
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]14
Định nghĩa một lớp chung trong Python
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]15
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]16
Quy tắc phạm vi cho from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
5¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 được sử dụng trong các chức năng chung khác nhau sẽ được suy ra là các loại khác nhau
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]17
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]18
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 được sử dụng trong một lớp chung sẽ được suy ra là cùng loại
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]19
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]20
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 được sử dụng trong một phương thức nhưng không khớp với bất kỳ tham số nào khai báo trongfrom typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
7 có thể được suy ra là các loại khác nhau
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]21
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]22
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]
5 không được xuất hiện trong phần thân của phương thức/hàm nếu nó là loại không liên kết
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]23
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]24
Giới hạn trong một tập hợp cố định các loại có thể¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]68 có nghĩa là chúng tôi tạo một biến loại có giới hạn giá trị
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]25
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]26
from typing import ClassVar, Dict, List
class Foo:
x: int = 1 # instance variable. default = 1
y: ClassVar[str] = "class var" # class variable
def __init__[self] -> None:
self.i: List[int] = [0]
def foo[self, a: int, b: str] -> Dict[int, str]:
return {a: b}
foo = Foo[]
foo.x = 123
print[foo.x]
print[foo.i]
print[Foo.y]
print[foo.foo[1, "abc"]]
5 có giới hạn trên¶
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]70 có nghĩa là chúng tôi tạo một biến loại có giới hạn trên. Khái niệm này tương tự như tính đa hình trong C++
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]27
Giống như c++, tạo một lớp cơ sở và
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]5 liên kết với lớp cơ sở. Sau đó, trình kiểm tra kiểu tĩnh sẽ lấy mọi lớp con làm kiểu lớp cơ sở
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]28
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]29
@overload¶
Đôi khi, chúng tôi sử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]72 để suy ra rằng trả về của hàm có nhiều loại khác nhau. Tuy nhiên, trình kiểm tra loại không thể phân biệt loại nào chúng ta muốn. Do đó, đoạn mã sau cho thấy trình kiểm tra loại không thể xác định loại nào là đúng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]30
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]31
Mặc dù chúng ta có thể sử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]73 để giải quyết vấn đề nhưng nó không tránh khỏi lỗi chính tả và
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]73 không an toàn
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]32
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]33
Sử dụng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]75 có thể giải quyết vấn đề. Chúng ta có thể khai báo kiểu trả về một cách rõ ràng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]34
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]33
Cảnh báo
Dựa trên PEP 484, trình trang trí
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]75 chỉ dành cho trình kiểm tra kiểu, nó không triển khai quá tải thực sự như c++/java. Vì vậy, chúng ta phải triển khai một chức năng chính xác không phải ____175. Trong thời gian chạy, gọi hàm
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]75 sẽ tăng
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]79
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]36
đầu ra
from typing import ClassVar, Dict, List class Foo: x: int = 1 # instance variable. default = 1 y: ClassVar[str] = "class var" # class variable def __init__[self] -> None: self.i: List[int] = [0] def foo[self, a: int, b: str] -> Dict[int, str]: return {a: b} foo = Foo[] foo.x = 123 print[foo.x] print[foo.i] print[Foo.y] print[foo.foo[1, "abc"]]37
Tập tin sơ khai¶
Các tệp sơ khai giống như các tệp tiêu đề mà chúng ta thường sử dụng để xác định giao diện của mình trong c/C++. Trong python, chúng ta có thể định nghĩa các giao diện của mình trong cùng một thư mục mô-đun hoặc
# errors will not be detected until runtime $ python fib.py Traceback [most recent call last]: File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 8, in print[[n for n in fib[3.5]]] File "fib.py", line 3, in fib for _ in range[n]: TypeError: 'float' object cannot be interpreted as an integer00