Python类型注解与typing的使用(转)
转自:https://liaolei.cc/?p=99
众所周知, Python是一种动态语言 在声明一个变量时我们不需要显示的声明它的类型.
类型注释可以提高代码的可读性和易用性, 帮助开发者写出更加严谨的代码, 让调用者减少类型方面的错误, 但是, 类型注解语法传入的类型表述有限, 不能说明复杂的类型组成情况, 因此引入了typing
模块, 用来实现复杂的类型表述
一、类型注解
1.具体语法
- 在声明变量类型时,变量后方紧跟一个冒号,冒号后面跟一个空格,再跟上变量的类型.
- 在声明方法返回值的时候,箭头左边是方法定义,箭头右边是返回值的类型,箭头左右两边都要留有空格.
1 2 3 | a: int = 2 def add(a: int ) - > int : pass |
二、typing
类型检查,防止运行时出现参数、返回值类型不符。
作为开发文档附加说明,方便使用者调用时传入和返回参数类型。
模块加入不会影响程序的运行不会报正式的错误,pycharm
支持typing检查错误时会出现黄色警告。
1.基础用法
2.类型
Type | Description |
---|---|
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
Tuple
Dict、Mapping、MutableMapping
Dict、字典,是 dict 的泛型;Mapping,映射,是 collections.abc.Mapping 的泛型。
- Dict 推荐用于注解返回类型
- Mapping 推荐用于注解参数
- MutableMapping 则是 Mapping 对象的子类,在很多库中也经常用 MutableMapping 来代替 Mapping。
Set、AbstractSet
- Set 推荐用于注解返回类型
- AbstractSet 用于注解参数
Sequence
在某些情况下,我们可能并不需要严格区分一个变量或参数到底是列表 list 类型还是元组 tuple 类型,我们可以使用一个更为泛化的类型,叫做 Sequence,其用法类似于 List
NoReturn
当一个方法没有返回结果时,为了注解它的返回类型,我们可以将其注解为 NoReturn
Any
一种特殊的类型,它可以代表所有类型,静态类型检查器的所有类型都与 Any 类型兼容,所有的无参数类型注解和返回类型注解的都会默认使用 Any 类型
TypeVar
自定义兼容特定类型的变量
NewType
声明一些具有特殊含义的类型
Callable
可调用类型,它通常用来注解一个方法
lambda类型标注
Union
联合类型,Union[X, Y]
代表要么是 X 类型,要么是 Y 类型。 联合类型的联合类型等价于展平后的类型
Optional
参数可以为空或已经声明的类型, 即 Optional[X]
等价于 Union[X, None]
。
不等价于可选参数,当它作为参数类型注解的时候,不代表这个参数可以不传递了,而是说这个参数可以传为 None
Generator
生成器类型, 声明方式, 其后的中括号紧跟着三个参数,分别代表 YieldType、SendType、ReturnType
- YieldType: yield 关键字后面紧跟的变量的类型
- SendType: yield 返回的结果的类型就是 SendType, 可为None
- ReturnType: 最后生成器 return 的内容类型, 可为None
三、实例
代码来源于 requests-html库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构