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的使用方式

相关推荐
IT方大同1 天前
C语言选择控制结构
c语言·开发语言
A24207349301 天前
js模糊搜索
开发语言·javascript·ecmascript
Learner__Q1 天前
每天五分钟:二分查找-LeetCode高频题解析_day4
python·算法·leetcode
Darkershadow1 天前
Python学习之使用pycharts
开发语言·python
晚秋大魔王1 天前
C语言-宏的基础、进阶、高级、内置宏的用法
c语言·开发语言·
写代码的【黑咖啡】1 天前
Python 中的控制流程:掌握程序的逻辑跳转
服务器·javascript·python
moxiaoran57531 天前
Go语言的递归函数
开发语言·后端·golang
水坚石青1 天前
Java+Swing+Mysql实现物业管理系统
java·开发语言·数据库·mysql·swing
尼古拉斯·纯情暖男·天真·阿玮1 天前
[JavaEE初阶] Thread类的基本用法
java·开发语言
特立独行的猫a1 天前
C++开发中的构建工具:现代CMake实战速成
开发语言·c++·cmake·入门教程