4.2 Python3 进阶 - 封包 / 解包

>>返回主目录


源码

a = 1, 2, 3
print(a, type(a))  # (1, 2, 3) <class 'tuple'>


# 当函数返回多个数值时,也会进行封包
def test():
    return 1, 2, 3


a = test()
print(a, type(a))  # (1, 2, 3) <class 'tuple'>

源码

# 解包
# 接收函数返回值
def test():
    return 1, 2, 3


a, b, c = test()  # 使用变量接收
print(a, b, c)
a, *b = test()  # 使用*号接收
print(a, b)
# # 遍历字典
# for key, value in my_dic.items():
#     print(key, value)
# # 传递参数
# def test(*args, **kwargs):
#   pass
# 合并两个字典
dic_1, dic_2 = {'一': 1}, {'二': 2}
# print(*dic_1, {**dic_1})
dic_3 = {**dic_1, **dic_2}
print(dic_3)  # 结果:{'一': 1, '二': 2}

>>返回主目录

posted @ 2021-05-28 11:13  PortosHan  阅读(144)  评论(0编辑  收藏  举报