Python学习第三十七天—函数3

[复制链接]
15271953841 发表于 2024-3-1 06:44:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
print("小姐姐")
小姐姐
print("小姐姐","爱","编程")    (参数,用户爱传几个就传几个,拥有这种特性的形参叫收集参数)
小姐姐 爱 编程
def myfunc(*args):           (定义收集参数,只需要形参的名字前面加上一个“*”来表示就可以了)
    print("有{}个参数,".format(len(args)))
    print("第2个参数是:{}".format(args[1]))

   
myfunc("小帅哥","小姐姐")
有2个参数,
第2个参数是:小姐姐
def myfunc(*args):           (把形参直接打印出来,看是否会原形毕露)
    print(args)

   
myfunc(1,2,3,4,5)            (背后立功的竟然是元组)
(1, 2, 3, 4, 5)
def myfunc():
    return 1,2,3

myfunc()
(1, 2, 3)                   (返回多个值,Python其实是利用了元组进行了打包)
x,y,z=myfunc()             (把拿到的返回值进行解包)
x
1
y
2
z
3
def myfunc(*args):
    print(type(args))

   
myfunc(1,2,3,4,5)             (把多个参数打包到元组里)
<class 'tuple'>
def myfunc(*args,a,b):            (如果收集参数后面还要指定其它参数,在调用函数时应该使用关键字参数来指定后面的参数,否则Python就会把实参纳入到收集参数中)
    print(args,a,b)

   
myfunc(1,2,3,4,5)
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    myfunc(1,2,3,4,5)
TypeError: myfunc() missing 2 required keyword-only arguments: 'a' and 'b'
myfunc(1,2,3,a=4,b=5)
(1, 2, 3) 4 5
def abc(a,*,b,c):                    (“*”号是一个匿名的收集参数,后面不加参数名的话,会被一起收集进去)
    print(a,b,c)

   
abc(1,2,3)
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    abc(1,2,3)
TypeError: abc() takes 1 positional argument but 3 were given
abc(1,b=2,c=3)
1 2 3
def myfunc(**kwargs):        (除了可以把多个参数打包成元组,收集参数还可以将参数打包成字典,作法就是使用两个连续的“**”号。)
    print(kwargs)

   
myfunc(a=1,b=2,c=3)
{'a': 1, 'b': 2, 'c': 3}
def myfunc(a,*b,**c):
    print(a,b,c)

   
myfunc(1,2,3,4,x=5,y=6)
1 (2, 3, 4) {'x': 5, 'y': 6}

help(str.format)              (format它可以拥有两种收集,同时使用一个“*”号,2个“**”号作为收集参数情况的方法)
Help on method_descriptor:

format(...)
    S.format(*args, **kwargs) -> str

    Return a formatted version of S, using substitutions from args and kwargs.
    The substitutions are identified by braces ('{' and '}').

def myfunc(*args,**kwargs):
    print("args={}".format(args))
    print("kwargs={}".format(kwargs))

   
myfunc(1,2,3,4)
args=(1, 2, 3, 4)
kwargs={}
myfunc(a=1,b=2,c=3)
args=()
kwargs={'a': 1, 'b': 2, 'c': 3}
myfunc(1,2,3,4,a=1,b=2,c=3)
args=(1, 2, 3, 4)
kwargs={'a': 1, 'b': 2, 'c': 3}
解包参数:一个“*”号或两个“*”号不仅在函数定义时可以使用,在函数调用时也是有特殊的效果的,在形参上使用我们称之为参数的打包,在实参上使用,则起到了相反的结果,就是解包参数)
args=(1,2,3,4)
def myfunc(a,b,c,d):
    print(a,b,c,d)

   
myfunc(args)
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    myfunc(args)                         (报错原因:需要4个参数,你只给它了一个参数)
TypeError: myfunc() missing 3 required positional arguments: 'b', 'c', and 'd'
myfunc(*args)                (利用“*”号对元组进行解包,它在传递的过程中,它不再是一个元组,它被解包成1,2,3,4分别传递到a,b,c,d4个形参里面)
1 2 3 4
kwargs={"a":1,"b":2,"c":3,"d":4}                  (这里的引号””不能省略)
myfunc(**kwargs)
1 2 3 4

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鄂ICP备13007585号-4

在本发帖
关注公众号
QQ客服返回顶部