Python中函数参数设置及使用的学习笔记

WBOY
Release: 2016-06-10 15:05:04
Original
1257 people have browsed it

一、参数和共享引用:

In [56]: def changer(a,b):
  ....:   a=2
  ....:   b[0]='spam'
  ....:   
In [57]: X=1
In [59]: L=[1,2]
In [60]: changer(X,L)
In [61]: X,L
Out[61]: (1, ['spam', 2])
Copy after login

函数参数是赋值得来,在调用时通过变量实现共享对象,函数中对可变对象 参数的在远处修能够影响调用者。

避免可变参数修改:

In [67]: X=1
In [68]: a=X
In [69]: a=2
In [70]: print(X)
1
In [71]: L=[1,2]
In [72]: b=L
In [73]: b[0]='spam'
In [74]: print(L)
['spam', 2]
In [75]: changer(X,L[:]) 
#不想要函数内部在原处的修改影响传递给它的对象,可以创建一个对象的拷贝

In [77]: changer(a,b)
In [78]: def changer(a,b): 
....:   b=b[:] 
#如果不想改变传入对象,无论函数怎么调用,同样可以在函数内部进行拷贝。
....:   
In [79]: a=2
In [80]: b[0]='spam'

Copy after login

二、特定参数匹配模型:

函数匹配语法:

201653183637382.png (811×216)

例子:

关键字参数:

In [2]: def f(a,b,c):print (a,b,c)
In [3]: f(1,2,3) #位置参数调用
(1, 2, 3)
In [4]: f(c=3,b=2,a=1) #关键字参数调用
(1, 2, 3)
Copy after login

默认参数:

In [5]: def f(a,b=2,c=3):print (a,b,c)
In [6]: f(1)  #给a赋值,b,c使用默认赋值 
(1, 2, 3)
In [7]: f(a=1) 
(1, 2, 3)
In [8]: f(1,4) 
(1, 4, 3)
In [9]: f(1,4,5) #不适用默认值
(1, 4, 5)
In [10]: f(1,c=6) #a通过位置得到1,b使用默认值,c通过关键字得到6
(1, 2, 6)
Copy after login

三、任意参数:

1、收集参数:

#*和**出现在函数定义或函数调用中。

In [11]: def f(*args):print (args)
In [12]: f()  #将所有位置相关的参数收集到一个新的元祖中
()
In [13]: f(1)
(1,)
In [14]: f(1,2,3,4)
(1, 2, 3, 4)
In [15]: def f(**args):print (args)
In [16]: f() 
{}
In [17]: f(a=1,b=2) #**只对关键字参数有效
{'a': 1, 'b': 2}

In [19]: def f(a, *pargs,**kargs):print(a,pargs,kargs)
In [20]: f(1,2,3,4,5,6,x=1,y=2,z=3)
(1, (2, 3, 4, 5, 6), {'y': 2, 'x': 1, 'z': 3})

Copy after login

2、解包参数:

注意:不要混淆函数头部或函数调用时*/**的语法:在头部意味着收集任意数量的参数,而在调用时,它接驳任意数量的参数。

In [21]: def func(a,b,c,d):print(a,b,c,d)
In [22]: args=(1,2)
In [23]: args += (3,4)
In [24]: func(*args)
(1, 2, 3, 4)
In [25]: args={'a':1,'b':2,'c':3}
In [26]: args['d']=4
In [27]: func(**args)
(1, 2, 3, 4)
In [28]: func(*(1,2),**{'d':4,'c':4})
(1, 2, 4, 4)
In [30]: func(1,*(2,3),**{'d':4})
(1, 2, 3, 4)
In [31]: func(1,c=3,*(2,),**{'d':4})
(1, 2, 3, 4)
In [32]: func(1,*(2,3,),d=4)
(1, 2, 3, 4)
In [33]: func(1,*(2,),c=3,**{'d':4})
(1, 2, 3, 4)
Copy after login

3、应用函数通用性:

In [34]: def tracer(func,*pargs,**kargs):
  ....: print ('calling:',func.__name__)
  ....: return func(*pargs,**kargs)
  ....: 
In [35]: def func(a,b,c,d):
  ....: return a+b+c+d
  ....: print (tracer(func,1,2,c=3,d=4))
  ....: 
('calling:', 'func')
10
Copy after login

4、python3.X中废弃apply内置函数

In [36]: pargs=(1,2)
In [37]: kargs={'a':3,'b':4}
In [41]: def echo(*args,**kargs):print (args,kargs)
In [42]: apply(echo,pargs,kargs)
((1, 2), {'a': 3, 'b': 4})
Copy after login

运用解包调用语法,替换:

In [43]: echo(*pargs,**kargs)
((1, 2), {'a': 3, 'b': 4})
In [44]: echo(0,c=5,*pargs,**kargs)
((0, 1, 2), {'a': 3, 'c': 5, 'b': 4})
Copy after login

四、python3.x中Keyword-only参数

python3.x把函数头部的排序规则通用化了,允许我们指定keyword-only参数,即按照关键字传递并且不会由一个位置参数来填充的参数;参数*args之后,必须调用关键字语法来传递。

In [1]: def kwonly(a,*b,c):
  ...: print(a,b,c) 
In [2]: kwonly(1,2,c=3)
1 (2,) 3
In [3]: kwonly(a=1,c=3)
1 () 3
In [4]: kwonly(1,2,3) #c必须按照关键字传递
TypeError: kwonly() missing 1 required keyword-only argument: 'c'

In [6]: def kwonly(a,*,b,c):print(a,b,c)
In [7]: kwonly(1,c=3,b=2)
1 2 3
In [8]: kwonly(c=3,b=2,a=1)
1 2 3
In [9]: kwonly(1,2,3)
TypeError: kwonly() takes 1 positional argument but 3 were given

Copy after login

1、排序规则:

**不能独自出现在参数中,如下都是错误用法:

In [11]: def kwonly(a,**pargs,b,c):
  ....: 
 File "<ipython-input-11-177c37879903>", line 1
def kwonly(a,**pargs,b,c):  ^
SyntaxError: invalid syntax

In [13]: def kwonly(a,**,b,c):
  ....: 
 File "<ipython-input-13-46041ada2700>", line 1
def kwonly(a,**,b,c):
  ^
SyntaxError: invalid syntax

Copy after login

也就是说一个函数头部,keyword-only参数必须编写在*args任意关键字形式之前,或者出现在args之前或者之后,并且可能包含在**args中。

In [14]: def f(a,*b,**d,c=6):print(a,b,c,d)
 File "<ipython-input-14-43c901fce151>", line 1
def f(a,*b,**d,c=6):print(a,b,c,d)
 ^
SyntaxError: invalid syntax
In [15]: def f(a,*b,c=6,**d):print(a,b,c,d) #keyword-only在*args之后,**args之前
In [16]: f(1,2,3,x=4,y=5)
1 (2, 3) 6 {'x': 4, 'y': 5}

In [20]: f(1,c=7,*(2,3),**dict(x=4,y=5)) #keyword-only在
1 (2, 3) 7 {'x': 4, 'y': 5}
In [21]: f(1,*(2,3),**dict(x=4,y=5,c=7))
1 (2, 3) 7 {'x': 4, 'y': 5}

Copy after login

2、为什么使用keyword-only参数?

很容易允许一个函数既接受任意多个要处理的位置参数,也接受作为关键字传递的配置选项, 可以减少代码,如果没有它的话,必须使用*args和**args,并且手动地检查关键字。

3、min调用

编写一个函数,能够计算任意参数集合和任意对象数据类型集合中的最小值。

方法一:使用切片

In [23]: def min(*args):
  ....: res=args[0]
  ....: for arg in args[1:]:
  ....: if arg < res:
  ....: res = arg
  ....: return res
  ....:
Copy after login

方法二:让python自动获取,避免切片。

In [28]: def min2(first,*rest):
  ....: for arg in rest:
  ....: if arg < first:
  ....: first = arg
  ....: return first
  ....:
Copy after login

方法三:调用内置函数list,将元祖转换为列表,然后调用list内置的sort方法实现。 注意:因为python sort列程是以C写出的,使用高度优化算法,运行速度要比前2中快很多。

In [32]: def min3(*args):
  ....: tmp=list(args)
  ....: tmp.sort()
  ....: return tmp[0]
  ....:

In [29]: min2(3,*(1,2,3,4))
Out[29]: 1
In [31]: min(*(5,6,6,2,2,7))
Out[31]: 2
In [33]: min3(3,4,5,5,2)
Out[33]: 2

Copy after login

五、例子:

1、模拟通用set函数:

编写一个函数返回两个序列的公共部分,编写inter2.py文件如下:

#!/usr/bin/python3
def intersect(*args):
  res=[]
  for x in args[0]:
    for other in args[1:]:
      if x not in other: break
    else:
      res.append(x)
  return res
def union(*args):
  res=[]
  for seq in args:
    for x in seq:
      if not x in res:
        res.append(x)
  return res
Copy after login

测试:

In [3]: from inter2 import intersect,union
In [4]: s1,s2,s3="SPAM","SCAM","SLAM"
In [5]: intersect(s1,s2),union(s1,s2)
Out[5]: (['S', 'A', 'M'], ['S', 'P', 'A', 'M', 'C'])
In [6]: intersect([1,2,3],(1,4))
Out[6]: [1]
In [7]: intersect(s1,s2,s3)
Out[7]: ['S', 'A', 'M']
In [8]: union(s1,s2,s3)
Out[8]: ['S', 'P', 'A', 'M', 'C', 'L']
Copy after login

2、模拟python 3.x print函数

编写文件python30.py

(1)使用*args和**args方法

环境python2.7

#!/usr/bin/python
import sys
def print30(*args,**kargs):
  sep = kargs.get('sep',' ')
  end = kargs.get('end','\n')
  file = kargs.get('file',sys.stdout)
  if kargs:raise TypeError('extra keywords: %s' %kargs)
  output = ''
  first = True
  for arg in args:
    output += ('' if first else sep)+str(arg)
    first = False
  file.write(output + end)
Copy after login

交互结果:

In [5]: print30(1,2,3)
1 2 3
In [6]: print30(1,2,3,sep='')
123
In [7]: print30(1,2,3,sep='...')
1...2...3
In [8]: print30(1,[2],(3,),sep='...')
1...[2]...(3,)
In [9]: print30(4,5,6,sep='',end='')
456
In [11]: print30(1,2,3)
1 2 3

In [12]: print30()

Copy after login

(2)使用keyword-only方法,实现效果和方法一一样:

#!/usr/bin/python3
import sys
def print30(*args,sep=' ',end='\n',file=sys.stdout):
  output = ''
  first=True
  for arg in args:
    output += ('' if first else sep) + str(arg)
    first = False
  file.write(output + end)
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!