Chú thích biến trong Python là gì?

Tài liệu này là một bảng gian lận nhanh cho biết cách sử dụng chú thích loại cho các loại phổ biến khác nhau trong Python

Biến

Về mặt kỹ thuật, nhiều chú thích loại hiển thị bên dưới là dư thừa, vì mypy thường có thể suy ra loại biến từ giá trị của nó. Xem để biết thêm chi tiết

# This is how you declare the type of a variable
age: int = 1

# You don't need to initialize a variable to annotate it
a: int  # Ok (no value at runtime until assigned)

# Doing so is useful in conditional branches
child: bool
if age < 18:
    child = True
else:
    child = False

Các loại tích hợp hữu ích

# For most types, just use the name of the type
x: int = 1
x: float = 1.0
x: bool = True
x: str = "test"
x: bytes = b"test"

# For collections on Python 3.9+, the type of the collection item is in brackets
x: list[int] = [1]
x: set[int] = {6, 7}

# For mappings, we need the types of both keys and values
x: dict[str, float] = {"field": 2.0}  # Python 3.9+

# For tuples of fixed size, we specify the types of all the elements
x: tuple[int, str, float] = (3, "yes", 7.5)  # Python 3.9+

# For tuples of variable size, we use one type and ellipsis
x: tuple[int, ...] = (1, 2, 3)  # Python 3.9+

# On Python 3.8 and earlier, the name of the collection type is
# capitalized, and the type is imported from the 'typing' module
from typing import List, Set, Dict, Tuple
x: List[int] = [1]
x: Set[int] = {6, 7}
x: Dict[str, float] = {"field": 2.0}
x: Tuple[int, str, float] = (3, "yes", 7.5)
x: Tuple[int, ...] = (1, 2, 3)

from typing import Union, Optional

# On Python 3.10+, use the | operator when something could be one of a few types
x: list[int | str] = [3, 5, "test", "fun"]  # Python 3.10+
# On earlier versions, use Union
x: list[Union[int, str]] = [3, 5, "test", "fun"]

# Use Optional[X] for a value that could be None
# Optional[X] is the same as X | None or Union[X, None]
x: Optional[str] = "something" if some_condition() else None
# Mypy understands a value can't be None in an if-statement
if x is not None:
    print(x.upper())
# If a value can never be None due to some invariants, use an assert
assert x is not None
print(x.upper())

Chức năng

from typing import Callable, Iterator, Union, Optional

# This is how you annotate a function definition
def stringify(num: int) -> str:
    return str(num)

# And here's how you specify multiple arguments
def plus(num1: int, num2: int) -> int:
    return num1 + num2

# If a function does not return a value, use None as the return type
# Default value for an argument goes after the type annotation
def show(value: str, excitement: int = 10) -> None:
    print(value + "!" * excitement)

# This is how you annotate a callable (function) value
x: Callable[[int, float], float] = f

# A generator function that yields ints is secretly just a function that
# returns an iterator of ints, so that's how we annotate it
def g(n: int) -> Iterator[int]:
    i = 0
    while i < n:
        yield i
        i += 1

# You can of course split a function annotation over multiple lines
def send_email(address: Union[str, list[str]],
               sender: str,
               cc: Optional[list[str]],
               bcc: Optional[list[str]],
               subject: str = '',
               body: Optional[list[str]] = None
               ) -> bool:
    ...

# Mypy understands positional-only and keyword-only arguments
# Positional-only arguments can also be marked by using a name starting with
# two underscores
def quux(x: int, / *, y: int) -> None:
    pass

quux(3, y=5)  # Ok
quux(3, 5)  # error: Too many positional arguments for "quux"
quux(x=3, y=5)  # error: Unexpected keyword argument "x" for "quux"

# This says each positional arg and each keyword arg is a "str"
def call(self, *args: str, **kwargs: str) -> str:
    reveal_type(args)  # Revealed type is "tuple[str, ...]"
    reveal_type(kwargs)  # Revealed type is "dict[str, str]"
    request = make_request(*args, **kwargs)
    return self.do_api_query(request)

Các lớp học

class MyClass:
    # You can optionally declare instance variables in the class body
    attr: int
    # This is an instance variable with a default value
    charge_percent: int = 100

    # The "__init__" method doesn't return anything, so it gets return
    # type "None" just like any other method that doesn't return anything
    def __init__(self) -> None:
        ...

    # For instance methods, omit type for "self"
    def my_method(self, num: int, str1: str) -> str:
        return num * str1

# User-defined classes are valid as types in annotations
x: MyClass = MyClass()

# You can also declare the type of an attribute in "__init__"
class Box:
    def __init__(self) -> None:
        self.items: list[str] = []

# You can use the ClassVar annotation to declare a class variable
class Car:
    seats: ClassVar[int] = 4
    passengers: ClassVar[list[str]]

# If you want dynamic attributes on your class, have it
# override "__setattr__" or "__getattr__":
# - "__getattr__" allows for dynamic access to names
# - "__setattr__" allows for dynamic assignment to names
class A:
    # This will allow assignment to any A.x, if x is the same type as "value"
    # (use "value: Any" to allow arbitrary types)
    def __setattr__(self, name: str, value: int) -> None: ...

    # This will allow access to any A.x, if x is compatible with the return type
    def __getattr__(self, name: str) -> int: ...

a.foo = 42  # Works
a.bar = 'Ex-parrot'  # Fails type checking

Khi bạn bối rối hoặc khi mọi thứ trở nên phức tạp

from typing import Union, Any, Optional, TYPE_CHECKING, cast

# To find out what type mypy infers for an expression anywhere in
# your program, wrap it in reveal_type().  Mypy will print an error
# message with the type; remove it again before running the code.
reveal_type(1)  # Revealed type is "builtins.int"

# If you initialize a variable with an empty container or "None"
# you may have to help mypy a bit by providing an explicit type annotation
x: list[str] = []
x: Optional[str] = None

# Use Any if you don't know the type of something or it's too
# dynamic to write a type for
x: Any = mystery_function()
# Mypy will let you do anything with x!
x.whatever() * x["you"] + x("want") - any(x) and all(x) is super  # no errors

# Use a "type: ignore" comment to suppress errors on a given line,
# when your code confuses mypy or runs into an outright bug in mypy.
# Good practice is to add a comment explaining the issue.
x = confusing_function()  # type: ignore  # confusing_function won't return None here because ...

# "cast" is a helper function that lets you override the inferred
# type of an expression. It's only for mypy -- there's no runtime check.
a = [4]
b = cast(list[int], a)  # Passes fine
c = cast(list[str], a)  # Passes fine despite being a lie (no runtime check)
reveal_type(c)  # Revealed type is "builtins.list[builtins.str]"
print(c)  # Still prints [4] .. the object is not changed or casted at runtime

# Use "TYPE_CHECKING" if you want to have code that mypy can see but will not
# be executed at runtime (or to have code that mypy can't see)
if TYPE_CHECKING:
    import json
else:
    import orjson as json  # mypy is unaware of this

Trong một số trường hợp, chú thích loại có thể gây ra sự cố trong thời gian chạy, hãy xem cách xử lý vấn đề này

Các loại vịt tiêu chuẩn

Trong mã Python điển hình, nhiều hàm có thể lấy một danh sách hoặc một lệnh làm đối số chỉ cần đối số của chúng là “giống như danh sách” hoặc “giống như chính tả” bằng cách nào đó. Một ý nghĩa cụ thể của “list-like” hoặc “dict-like” (hoặc something-other-like) được gọi là “duck type” và một số loại duck phổ biến trong Python thành ngữ được chuẩn hóa

Chú thích biến là gì?

Chú thích biến về cơ bản là cải tiến của gợi ý kiểu , được giới thiệu trong Python 3. 5. Giải thích đầy đủ đằng sau Chú thích biến được giải thích trong PEP 526. Trong bài viết này, chúng tôi sẽ giới thiệu nhanh về gợi ý kiểu và sau đó giới thiệu cú pháp Chú thích biến mới.

Chú thích trong Python là gì?

Chú thích đã được giới thiệu trong Python 3. 0 ban đầu mà không có mục đích cụ thể. Chúng chỉ đơn giản là một cách để liên kết các biểu thức tùy ý với các đối số của hàm và trả về các giá trị . Nhiều năm sau, PEP 484 đã xác định cách thêm gợi ý kiểu vào mã Python của bạn, dựa trên công việc mà Jukka Lehtosalo đã thực hiện trên bằng tiến sĩ của mình. Đ.

Chú thích chức năng là gì và chúng được sử dụng như thế nào trong Python?

Chú thích hàm, cho cả tham số và giá trị trả về, là hoàn toàn tùy chọn. Chú thích hàm không gì khác hơn là một cách liên kết các biểu thức Python tùy ý với các phần khác nhau của hàm tại thời điểm biên dịch . Bản thân Python không gắn bất kỳ ý nghĩa hoặc tầm quan trọng cụ thể nào vào các chú thích.

Tôi có nên sử dụng chú thích kiểu trong Python không?

Nhập gợi ý giúp bạn xây dựng và duy trì kiến ​​trúc sạch 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