Nhập Python tùy chọn

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"]]
      
      3

    • Cẩ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"]]
      
      4

    • 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

    • 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

    • 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

    • Hạ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 integer
0

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"]]

# 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
1

đầ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
2

Đố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 integer
3

đầ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
4

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 integer
5

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 integer
6

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"]]

# 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
7

đầ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
8

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 integer
9

đầ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"]]

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"]]

  • 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 trong
    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 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 integer
00

Nhập tùy chọn trong Python là gì?

Sử dụng Tùy chọn để biểu thị rằng một đối tượng là một loại nhất định hoặc Không có . Ví dụ. từ việc gõ nhập Dict, Tùy chọn, Union dict_of_users. Dict[int, Union[int,str]] = { 1. "Jerome", 2. "Lewis", 3. 32 } user_id. Tùy chọn[int] user_id = Không # user_id hợp lệ = 3 # cũng hợp lệ user_id = "Xin chào" # không hợp lệ.

Việc gõ có được thực thi trong Python không?

Thời gian chạy Python không thực thi chú thích hàm và 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.

Gợi ý kiểu có cần thiết trong Python không?

Gợi ý nhập giúp bạn xây dựng và duy trì kiến ​​trúc gọn gàng hơn . Hành động viết các gợi ý về loại buộc bạn phải suy nghĩ về các loại trong chương trình của mình. Mặc dù bản chất động của Python là một trong những tài sản tuyệt vời của nó, nhưng ý thức về việc dựa vào kiểu gõ vịt, phương thức quá tải hoặc nhiều loại trả về là một điều tốt.

PEP 484 là gì?

PEP 484 – Gợi ý nhập - Đề xuất cải tiến Python . PEP này nhằm mục đích cung cấp một cú pháp tiêu chuẩn cho các chú thích loại, mở mã Python để phân tích và tái cấu trúc tĩnh dễ dàng hơn, thời gian chạy tiềm năng. Đọc thêm > Gõ gợi ý cheat sheet [Python 3] - mypy 0. tài liệu 982.

Chủ Đề