Python - Union联合类型注解

类型注解

Union类型注解

1.理解union类型

2.掌握使用union进行联合类型注解

Uion类型

python 复制代码
from typing import union
my_list: list[Union[str,int]] = [1,2,"itheima","itcast"]
my_dict: dict[str,Union[str,int]] = {"name":"周杰伦","age":13}

# 使用Union[类型,......,类型]
# 可以定义联合类型注解

Union联合类型注解,在变量注解、函数(方法)形参和返回值注解中,均可使用

python 复制代码
my_list: list[Union(int,str)] = [1,2,"itcast","itheima"]
my_dict: dict[str,Union[str, int]] = {"name":"周杰伦","age": 31}

def func(data: Union[int, str]) -> Union[int, str]:
	pass
python 复制代码
"""
	Union联合类型注解
""
# 使用Union类型,必须先导包
from typing import Union

my_list: list[Union[int,str]] = [1,2,"itheima","itcast"]

def func(data: Union[int,str]) -> Union(int, str):
	pass

func()

总结

1.什么是Union类型

使用Union可以定义联合类型注解

2.Unionz的使用方式

相关推荐
2301_8092047026 分钟前
JavaScript中严格模式use-strict对引擎解析的辅助.txt
jvm·数据库·python
zjy2777741 分钟前
mysql如何选择合适的索引类型_mysql索引设计实战
jvm·数据库·python
Aaswk42 分钟前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite1 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Cyber4K2 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee3 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
苍煜3 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
yong99903 小时前
MATLAB读取高光谱图像
开发语言·matlab
2zcode3 小时前
基于MATLAB的肝病风险评估与分期分析系统设计与实现
开发语言·matlab
小小de风呀4 小时前
de风——【从零开始学C++】(五):内存管理
开发语言·c++