Python中函数参数设置及使用的学习笔记
一、参数和共享引用:
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])
函数参数是赋值得来,在调用时通过变量实现共享对象,函数中对可变对象 参数的在远处修能够影响调用者。
避免可变参数修改:
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'
二、特定参数匹配模型:
函数匹配语法:
例子:
关键字参数:
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)
默认参数:
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)
三、任意参数:
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})
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)
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
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})
运用解包调用语法,替换:
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})
四、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
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
也就是说一个函数头部,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}
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 ....:
方法二:让python自动获取,避免切片。
In [28]: def min2(first,*rest): ....: for arg in rest: ....: if arg < first: ....: first = arg ....: return first ....:
方法三:调用内置函数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
五、例子:
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
测试:
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']
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)
交互结果:
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()
(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)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
