美文网首页
python 之单星号(*)与双星号(**)

python 之单星号(*)与双星号(**)

作者: 冲锋丘丘人 | 来源:发表于2024-06-04 11:10 被阅读0次

一、作为运算符

*表示乘法运算
**表示乘方运算

print(f"a = {3 * 2}")  # 输出6
print("b = {}".format(3 ** 2))  # 输出9

二、作为函数形参

*在python中做函数形参,表示的是一个可变长度的序列,类型为tuple,即将所有参数放入一个元组(tuple)中,被函数使用。
**在python中做函数形参,表示的是一个可变长度的序列,类型为dict,即将所有的关键字参数,放入一个字典(dict)中, 被函数使用。
*与**作为形参同时出现时,**必须在最后面

def printx(*arg1, **arg2):
    print("arg1={0}, type:{1}".format(arg1, type(arg1)))
    print("arg2={0}, type:{1}".format(arg2, type(arg2)))


P1 = printx(1, 2, key1=1, key2=2)
#output
#arg1=(1, 2), type:<class 'tuple'>
#arg2={'key1': 1, 'key2': 2}, type:<class 'dict'>

三、作为函数实参

在list/tuple/set前加*,会把序列中的所有元素解包成位置参数
在dict前加*,会把dict的键变成位置参数;加**,把字典的键值对变成关键字参数。
A = (a,b,c)
B = {“key1” :1, “key2”:2}
P3 = Printx(*A) # 等同于printx(a,b,c)
P3 = Printx(*A, *B) #等同于printx(a,b,c,key1,key2)
P3 = Printx(*A, **B) #等同于printx(a,b,c,key1=1,key2=2)

def printx(*arg1, **arg2):
    print("arg1={0}, type:{1}".format(arg1, type(arg1)))
    print("arg2={0}, type:{1}".format(arg2, type(arg2)))

A = (1, 2, 3)
K1 = {'key1': 1, 'key2': 2}
P2 = printx(*A, *K1)  # 注意在dict前加一个*,只把dict的 '键' 作为位置参传递
print('')
P3 = printx(*A, **K1)
#output
#arg1=(1, 2, 3, 'key1', 'key2'), type:<class 'tuple'>
#arg2={}, type:<class 'dict'>

#arg1=(1, 2, 3), type:<class 'tuple'>
#arg2={'key1': 1, 'key2': 2}, type:<class 'dict'>

四、作为序列解包

注意序列解包时*解包为list,不再是tuple了

  1. 先对不带的变量解包,剩余的数据在划分给带的变量
    x,y,*z = 1,2,3,4,5 #等同于 x=1, y = 2, z= [3,4,5]
    x,*y,z = 1,2,3,4,5 #等同于 x=1, y = [2,3,4], z= 5
  2. 如果左值比右值要多,那么带 * 的变量默认为空列表
    x,y,*z = 1,2 #等同于 x=1, y = 2, z= []
    x,*y,z = 1,2 #x=1, y = [], z= 2
  3. 单*只解包dict的键, **解包dict键值对
    key_A = {‘key1’:1, ‘key2’:2}
    key_B = {“key3” :3, “key4”:4}
    X = {*key_A} #等同于x= {‘key1’, ’key2’}
    X = {**key_A} #等同于x= {‘key1’:1, ‘key2’:2}
    X = {**key_A, **key_B} #等同于组包,x={‘key1’:1, ‘key2’:2, “key3” :3, “key4”:4}
# 4.解包测试
print('\n------解包测试------')
x, y, *z = 1, 2, 3, 4, 5
print("x={0}, y= {1}, z={2}".format(x, y, z))

x, *y, z = 1, 2, 3, 4, 5
print("x={0}, y= {1}, z={2}".format(x, y, z))

x, *y, z = 1, 2
print("x={0}, y= {1}, z={2}".format(x, y, z))

x, y, *z = 1, 2
print("x={0}, y= {1}, z={2}".format(x, y, z))

# 解包&组包
K2 = {'key3': 3, 'key4': 4}
K3 = {'key5': 5, 'key6': 6}
k4 = {**K2, **K3}
k5 = {*K2,*K3}
print("k4=", k4)
print("k5=", k5)
#output
#------解包测试------
#x=1, y= 2, z=[3, 4, 5]
#x=1, y= [2, 3, 4], z=5
#x=1, y= [], z=2
#x=1, y= 2, z=[]
#k4= {'key3': 3, 'key4': 4, 'key5': 5, 'key6': 6}
#k5= {'key6', 'key5', 'key4', 'key3'}

注意

在Python中默认的函数参数顺序是:必选参数、默认参数、*args和**kwargs。如下所示:

def  testFunc(name, age=10, *agrs, **kwargs):
    pass

相关文章

  • Markdown的学习

    1. 强调 星号与下划线都可以,单是斜体,双是粗体,符号可以跨行,符号可以空格 强调 单星号斜线斜线 2.分割线...

  • Markdown学习笔记

    强调 星号/单下划线把需强调文字括起来:斜体 双星号/双下划线把需强调文字括起来:粗体 分割线 分割线: 直接至少...

  • python中星号变量的特殊用法

    引言 在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",...

  • python里星号

    python里星号有两种意思 1. 定义函数时一般情况下,函数的参数接受指定个数的参数,比如def func(a,...

  • python学习:python的星号(*)和双星号(**)用法

    https://www.cnblogs.com/empty16/p/6229538.html

  • python中星号(*)和双星号(**)的用法

    一:数学运算符,*表示乘法,**表示取幂,如: 二:用在变量的前面。 1,向函数传递参数,将变量中可迭代对象的元素...

  • [Python] glob 模块(查找文件路径)

    通配符: 通配符-星号*:星号*匹配一个文件名段中的0个或多个字符 单配符-问号?:问号?会匹配文件名中该位置的单...

  • 第二课:List列表、Tuple元祖、Dict字典

    Python 列表List 加号+是列表连接运算符,星号*是重复操作 列表元素的添加与删除 输出: Python列...

  • 白话诗-《星号》

    一颗星号, 是消失的词汇; 一颗星号, 是无尽的遐想; 一颗星号, 是关上的大门; 一颗星号, 是闪亮的刺刀。 有...

  • 【JavaScript的注释】

    注释的种类:单行、多行 单行注释: // 调试使用; 多行注释: /* */ 星号对齐;每行开头加一个星号,星号后...

网友评论

      本文标题:python 之单星号(*)与双星号(**)

      本文链接:https://www.haomeiwen.com/subject/ppryqjtx.html