码迷,mamicode.com
首页 > 编程语言 > 详细

python函数调用时传参方式

时间:2018-02-25 00:09:38      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:就是   **kwargs   body   bsp   print   pos   opened   字典   def   

位置参数
位置参数需与形参一一对应
def test(a,b) #a,b就是位置参数
    print(a)
    print(b)

test(1,2)

 

 
关键字参数
与形参顺序无关
def test(x,y)
    print(x,y)
 
test(x=2,y=3)

 

位置参数必须在关键字参数之前
 
**kwargs:把N个关键字参数,转换成字典格式
技术分享图片
1 def test(a,**kwargs)
2     print(a)
3     print(kwargs)
4 test(8,c=1,b=2)
View Code

 

*args:把N个位置参数,转换成元组
def test(a,*args)
  print(a)   
print(args) test(1,2,3,4,5)

 

python函数调用时传参方式

标签:就是   **kwargs   body   bsp   print   pos   opened   字典   def   

原文地址:https://www.cnblogs.com/shangmo/p/8467870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!