• Python类型注解与typing的使用(转)


    转自:https://liaolei.cc/?p=99

    众所周知, Python是一种动态语言 在声明一个变量时我们不需要显示的声明它的类型.

    类型注释可以提高代码的可读性和易用性, 帮助开发者写出更加严谨的代码, 让调用者减少类型方面的错误, 但是, 类型注解语法传入的类型表述有限, 不能说明复杂的类型组成情况, 因此引入了typing模块, 用来实现复杂的类型表述

    一、类型注解

    1.具体语法

    • 在声明变量类型时,变量后方紧跟一个冒号,冒号后面跟一个空格,再跟上变量的类型.
    • 在声明方法返回值的时候,箭头左边是方法定义,箭头右边是返回值的类型,箭头左右两边都要留有空格.
    a: int = 2
    def add(a: int) -> int:
        pass
    

    二、typing

    类型检查,防止运行时出现参数、返回值类型不符。

    作为开发文档附加说明,方便使用者调用时传入和返回参数类型。

    模块加入不会影响程序的运行不会报正式的错误,pycharm支持typing检查错误时会出现黄色警告。

    1.基础用法

    from typing import List, Tuple, Dict
    
    names: List[str] = ["li", "tom"]
    version: Tuple[int, int, int] = (6, 6, 6)
    operations: Dict[str, bool] = {'sad': False, 'happy': True}
    
    Python

    2.类型

    TypeDescription
    int 整型
    float 浮点数字
    bool 布尔
    str 字符串
    bytes 8位字符
    object 任意对象
    List(str) 字符串组成的列表
    Tuple[int, ...] 任意数量的int对象的元组
    Tuple[int, int] 两个int对象的元组
    Dict[str, int] 键是 str 值是 int 的字典
    Iterable[int] 包含 int 的可迭代对象
    Sequence[bool] 布尔值序列(只读)
    Mapping[str, int] 从 str 键到 int 值的映射(只读)
    Any 具有任意类型的动态类型值
    Union 联合类型
    Optional 参数可以为空或已经声明的类型
    Mapping 映射,是 collections.abc.Mapping 的泛型
    MutableMapping Mapping 对象的子类,可变
    Generator 生成器类型, Generator[YieldType、SendType、ReturnType]
    NoReturn 函数没有返回结果
    Set 集合 set 的泛型, 推荐用于注解返回类型
    AbstractSet collections.abc.Set 的泛型,推荐用于注解参数
    Sequence ollections.abc.Sequence 的泛型,list、tuple 等的泛化类型
    TypeVar 自定义兼容特定类型的变量
    NewType 声明一些具有特殊含义的类型
    Callable 可调用类型, Callable[[参数类型], 返回类型]

    List

    var: List[int or float] = [2, 3.5]
    var: List[List[int]] = [[1, 2], [2, 3]]
    
    Python

    Tuple

    person: Tuple[str, int, float] = ('Mike', 22, 1.75)
    
    Python

    Dict、Mapping、MutableMapping

    Dict、字典,是 dict 的泛型;Mapping,映射,是 collections.abc.Mapping 的泛型。

    • Dict 推荐用于注解返回类型
    • Mapping 推荐用于注解参数
    • MutableMapping 则是 Mapping 对象的子类,在很多库中也经常用 MutableMapping 来代替 Mapping。
    def size(rect: Mapping[str, int]) -> Dict[str, int]:
        return {'width': rect['width'] + 100, 'height': rect['width'] + 100}
    
    Python

    Set、AbstractSet

    • Set 推荐用于注解返回类型
    • AbstractSet 用于注解参数
    def describe(s: AbstractSet[int]) -> Set[int]:
        return set(s)
    
    Python

    Sequence

    在某些情况下,我们可能并不需要严格区分一个变量或参数到底是列表 list 类型还是元组 tuple 类型,我们可以使用一个更为泛化的类型,叫做 Sequence,其用法类似于 List

    def square(elements: Sequence[float]) -> List[float]:
        return [x ** 2 for x in elements]
    
    Python

    NoReturn

    当一个方法没有返回结果时,为了注解它的返回类型,我们可以将其注解为 NoReturn

    def hello() -> NoReturn:
        print('hello')
    
    Python

    Any

    一种特殊的类型,它可以代表所有类型,静态类型检查器的所有类型都与 Any 类型兼容,所有的无参数类型注解和返回类型注解的都会默认使用 Any 类型

    def add(a: Any) -> Any:
        return a + 1
    
    Python

    TypeVar

    自定义兼容特定类型的变量

    height = 1.75
    Height = TypeVar('Height', int, float, None)
    def get_height() -> Height:
        return height
    
    Python

    NewType

    声明一些具有特殊含义的类型

    Person = NewType('Person', Tuple[str, int, float])
    person = Person(('Mike', 22, 1.75))
    
    Python

    Callable

    可调用类型,它通常用来注解一个方法

    def date(year: int, month: int, day: int) -> str:
        return f'{year}-{month}-{day}'
    # 参数类型和返回值类型都要注解出来
    def get_date_fn() -> Callable[[int, int, int], str]:
        return date
    
    Python

    lambda类型标注

    is_even: Callable[[int], bool] = lambda x: (x % 2 == 0)
    
    Python

    Union

    联合类型,Union[X, Y] 代表要么是 X 类型,要么是 Y 类型。 联合类型的联合类型等价于展平后的类型

    Union[Union[int, str], float] == Union[int, str, float]
    Union[int] == int
    Union[int, str, int] == Union[int, str]
    Union[int, str] == Union[str, int]
    
    Python
    def process(: Union[str, Callable]):
         isinstance(, str):
            # str2fn and process
            pass
         isinstance(, Callable):
            ()
    
    Python

    Optional

    参数可以为空或已经声明的类型, 即 Optional[X] 等价于 Union[X, None]

    不等价于可选参数,当它作为参数类型注解的时候,不代表这个参数可以不传递了,而是说这个参数可以传为 None

    # 当一个方法执行结果, 如果执行完毕就不返回错误信息, 如果发生问题就返回错误信息
    def judge(result: bool) -> Optional[str]:
        if result: return 'Error Occurred'
    
    Python

    Generator

    生成器类型, 声明方式, 其后的中括号紧跟着三个参数,分别代表 YieldType、SendType、ReturnType

    def echo_round() -> Generator[int, float, str]:
        sent = yield 0
        while sent >= 0:
            sent = yield round(sent)
        return 'Done'
    
    Python
    • YieldType: yield 关键字后面紧跟的变量的类型
    • SendType: yield 返回的结果的类型就是 SendType, 可为None
    • ReturnType: 最后生成器 return 的内容类型, 可为None

    三、实例

    代码来源于 requests-html

    # Typing.
    _Find = Union[List['Element'], 'Element']
    _XPath = Union[List[str], List['Element'], str, 'Element']
    _Result = Union[List['Result'], 'Result']
    _HTML = Union[str, bytes]
    _BaseHTML = str
    _UserAgent = str
    _DefaultEncoding = str
    _URL = str
    _RawHTML = bytes
    _Encoding = str
    _LXML = HtmlElement
    _Text = str
    _Search = Result
    _Containing = Union[str, List[str]]
    _Links = Set[str]
    _Attrs = MutableMapping
    _Next = Union['HTML', List[str]]
    _NextSymbol = List[str]
    
    
    class Element(BaseParser):
        """An element of HTML.
    
        :param element: The element from which to base the parsing upon.
        :param url: The URL from which the HTML originated, used for ``absolute_links``.
        :param default_encoding: Which encoding to default to.
        """
    
        __slots__ = [
            'element', 'url', 'skip_anchors', 'default_encoding', '_encoding',
            '_html', '_lxml', '_pq', '_attrs', 'session'
        ]
    
        def __init__(self, *, element, url: _URL, default_encoding: _DefaultEncoding = None) -> None:
            super(Element, self).__init__(element=element, url=url, default_encoding=default_encoding)
            self.element = element
            self.tag = element.tag
            self.lineno = element.sourceline
            self._attrs = None
    
        def __repr__(self) -> str:
            attrs = ['{}={}'.format(attr, repr(self.attrs[attr])) for attr in self.attrs]
            return "<Element {} {}>".format(repr(self.element.tag), ' '.join(attrs))
    
        @property
        def attrs(self) -> _Attrs:
            """Returns a dictionary of the attributes of the :class:`Element <Element>`
            (`learn more <https://www.w3schools.com/tags/ref_attributes.asp>`_).
            """
            if self._attrs is None:
                self._attrs = {k: v for k, v in self.element.items()}
    
                # Split class and rel up, as there are usually many of them:
                for attr in ['class', 'rel']:
                    if attr in self._attrs:
                        self._attrs[attr] = tuple(self._attrs[attr].split())
    
            return self._attrs
    

      

  • 相关阅读:
    超级强大的SVG SMIL animation动画详解
    Java Math的 floor,round和ceil的总结
    理解SVG的viewport,viewBox,preserveAspectRatio
    SVG-1
    PHP通过exec执行git pull
    Nodejs中request出现ESOCKETTIMEDOUT解决方案
    Linux安装了mysql 无法远程连接
    linux下docker启动nginx无法访问80端口
    ZPL文件打印
    k8s结合helm部署
  • 原文地址:https://www.cnblogs.com/luhouxiang/p/16041133.html
Copyright © 2020-2023  润新知