当前位置: 代码网 > it编程>前端脚本>Python > python的type hints(类型标注、类型注解、类型提示)示例详解

python的type hints(类型标注、类型注解、类型提示)示例详解

2024年08月13日 Python 我要评论
1. 前言type hints(类型标注、类型注解、类型提示)是指在代码中涉及到数据交互的地方,提供数据类型的注解(显式的说明)。类型注解可以帮助第三方ide工具(pycharm)对代码进行类型推断,

1. 前言

type hints(类型标注、类型注解、类型提示)是指在代码中涉及到数据交互的地方,提供数据类型的注解(显式的说明)。类型注解可以帮助第三方ide工具(pycharm)对代码进行类型推断,协助做代码提示;能帮助开发者自身对变量进行类型标注。类型注解可分为:变量的类型注解、函数(方法)形参列表和返回值的类型注解。总而言之,加上注解之后,我们可以直观的看出变量、函数(方法)参数列表和返回值到底是个什么东西(什么类型)。

2. 简单的类型注解

简单的类型注解(基本类型注解):可以使用int、str和list等基本类型来注解变量、函数参数和返回值的类型。

2.1 变量的类型注解

为变量设置类型注解,语法格式为:变量: 类型,代码示例如下。

num: int = 10
s: str = "hello"
my_list: list = [1,2,3,4]
my_tuple: tuple = (1,2,3)

class student:
	pass
stu: student = student()

除了使用变量: 类型这种格式外,还可以在注释中进行类型注解,语法:# type: 类型,示例代码如下。

num = 10  # type: int
s = "hello"  # type: str
my_list = [1,2,3,4]  # type: list
my_tuple = (1,2,3)  # type: tuple

2.2 函数(方法)的类型注解

给函数(方法)形参类型和返回值类型进行标注,定义语法如下:

def 函数(或方法)名(形参1: 类型, 形参2: 类型, ...) -> 返回值类型:
    pass

示例代码如下。函数注解的信息,保存在__annotations__属性中可以来调用。

def sum(x: int, y: int) -> int:
    return x+y
    
sum(1,2)
print(sum.__annotations__)
# 输出:{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}

2.3 安装mypy

注意:类型注解仅仅是提示性的,不是决定性的,就算你类型注解错误也不会报错。像下面的代码就不会报错:变量num的类型注解为int,却把字符串"hello"赋给它了;变量s的类型注解为str,却把整数10赋给它了;函数sum的形参x,y和返回值都是int类型,却把字符串"hello"和"world"分别赋给参数x,y。

num: int = "hello"
s: str = 10

def sum(x: int, y: int) -> int:
    return x+y

print(sum(1,2))
print(sum("hello ","world"))

我们可以通过mypy库来检验最终代码是否符合设定的类型注解。mypy是python中有关类型的静态分析工具。

#安装 mypy
pip install mypy

# 执行下面代码来查看是否符合设定的类型注解
mypy test.py

3. 标准库typing模块

上面只是简单的类型注解,传入的类型表述能力有限,不能说明复杂的类型组成情况,因此可以引入typing模块来实现复杂的类型表达,typing是在python 3.5 才开始有的模块。

3.1 list, tuple, set, dict基础用法

(1)list[int]表示由整数(int)组成的列表;

(2)tuple[int, str, bool]表示由三个对象组成的元组,其中这三个对象的类型分别为int, str, bool;

(3)set[int]表示由整数(int)组成的集合;

(4)dict[int, str]表示键是int,值是str的字典。

代码示例如下

from typing import list, tuple, set, dict

my_list: list[int] = [1, 2, 3, 4, 5, 6]
my_tuple: tuple[int, str, bool] = (1, "hi", true)
my_set: set[int] = {1, 2, 3}
my_dict: dict[int, str] = {1: "a", 2: "b", 3: "c"}

3.2 union和optional

对于更为复杂的类型注解,我们可以使用联合类型注解uniontyping模块中的union可以给一个变量或函数返回值标注多种类型,它表示的意思。例如:data: union[str, bytes],变量data可以是str类型或bytes类型。在python 3.10版本及以后,推荐使用|运算符代替union。示例代码如下:

from typing import union

my_list: list[union[int,str]] = [1,3,5,"hello,"world"]
my_dict: dict[str, union[str, int]]= {"name": "jack", "age": 16}

(1)list[union[str,int]]:该列表中的元素可以是int类型的对象,也可以是str类型的对象;

(2)dict[str, union[str, int]]:该字典中的键为str,值是str和int都可以。

  typing模块中的optional类型,它是union的一个特殊情况。如果一个变量可以是none或其他类型,推荐使用optional类型,其中optional[int]等价于union[int, none]optional[union[str, int, float]]等价于union[str, int, float, none]。对了,optional[]里面只能写一个数据类型,代码示例如下

num1: optional[union[int, str, float]] = none
num2: union[int, str, float,none] = none

3.3 类型别名(type alias)

因为在python中一切皆为对象,所以可将复杂一点的类型赋给某个变量,也就是起个别名,这样使用起来较为方便。

var_int = int
num1: int = 10
num2: var_int
# 以上两种类型注解的方式等价

from typing import list, dict

list_alias = list[int]
dict_alias = dict[str, list_alias]
my_dict: dict_alias = {"a": [1,2,3], "b": [4,5,6]}
# 以上两种类型注解的方式等价

3.4 callable

callable是一个可调用对象类型,语法格式为:callable[[参数类型], 返回类型],包括可调用对象的参数类型和返回值类型。例如:callable[[int], str],这里的int表示可调用对象的参数类型,str代表可调用对象的返回值类型。

from typing import callable

def print_name(name: str) -> none:
    print(f"name:{name}")

def get_name(func: callable[[str], none]):
    return func

# 利用isinstance(对象, callable)来判断当前对象是否为可调用对象
# print(isinstance(print_name, callable))  # 输出 true

fun = get_name(print_name)
fun("zhangsan")

3.5 typevar和generic

泛型是一种可以在不指定具体类型的情况下编写可复用代码的技术。python中的泛型是使用typing模块中的typevargeneric进行实现的。typevar用于定义泛型类型变量,而generic用于定义泛型类。

(1)泛型类型变量:用于表示不确定的类型。泛型类型变量通常使用大写字母命名,比如 t、u、v。

t = typevar('t')  # 创建一个泛型类型变量t,t可以是任意类型

(2)泛型函数:是可以接受一个或多个泛型类型参数的函数,这些参数可以用来指定函数参数的类型、返回值的类型或函数内部使用的其他类型。

from typing import typevar,list

t = typevar('t')

def print_list(my_list: list[t]) -> none:
    print(my_list)

print_list([2,4,6,8])  # 可以打印整数列表
print_list(["hh","aa","www"])  # 可以打印字符串列表

(3)约束泛型:有时候我们希望泛型类型只能是特定的类型,在这种情况下,我们可以使用泛型约束来限制泛型类型的范围。

from typing import typevar,list

t = typevar('t',int,float)  # 泛型类型变量t可以是int,也可以是float类型

def print_list(my_list: list[t]) -> none:
    print(my_list)

print_list([2,4,6,8])  # 可以打印整数列表
print_list([1.2, 2.3, 3.4])  # 可以打印浮点数列表

(4)泛型类:是可以接受一个或多个泛型类型参数的类。这些参数可以用来指定类的属性类型、方法参数类型、方法返回值类型或类内部使用的其他类型。

from typing import typevar, generic

t = typevar('t')

class student(generic[t]):  # 继承generic[t],
    def __init__(self, name: t) -> none:
        self.name = name

    def get_name(self) -> t:
        return self.name
    
stu = student[str]("zhangsan")  # 或者stu = student("zhangsan")
print(stu.get_name())  # 输出:zhangsan

3.6 any 类型

静态类型检查器将所有类型视为与any兼容,这意味着可对类型为any的值执行任何操作或方法调用,并将其赋值给任何变量。

from typing import any

a = 10 # 变量a在默认情况下为any类型

def print_info(var: any) -> none:
    print(var)

print_info(11)
print([1,2,3,4])
print("string")

最后再说一句:如果你按照规范的格式进行类型注解,却发现报错误,那很大可能是python版本太低。比如下面代码中的类型注解就是在python 3.10开始支持的,3.10版本以下的不支持该语法格式的类型注解。

x: int | float = 2 # 表示 or python 3.10 开始支持

参考文章:

总结 

到此这篇关于python的type hints(类型标注、类型注解、类型提示)的文章就介绍到这了,更多相关python类型提示type hints内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com