5.3.2 关键参数
>>> def demo(a,b,c = 5):
... print(a,b,c)
...
>>> demo(3,7)
3 7 5
>>> demo(a = 7,b = 3,c = 6)
7 3 6
>>> demo(c = 8,a = 9,b = 0)
9 0 8
5.3.3 可变长度参数
在这里插入代码片
>>> def demo(*p):
... print(p)
...
>>> demo(1,2,3)
(1, 2, 3)
>>> demo(1,2)
(1, 2)
2.**parameter用来接受关键参数并存放到字典中
>>> def demo(**p):
... for item in p.items():
... print(item)
...
>>> demo(x=1,y=2,c=3)
('x', 1)
('y', 2)
('c', 3)
>>> def func(a,b,c=4,*aa,**bb):
... print(a,b,c)
... print(aa)
... print(bb)
...
>>> func(1,2,3,4,5,6,7,8,9,xx='1',yy='2',zz=3)
1 2 3
(4, 5, 6, 7, 8, 9)
{'xx': '1', 'yy': '2', 'zz': 3}
5.3.4 参数传递的序列解包
>>> def demo(a,b,c):
... print(a+b+c)
...
>>> demo(1,2,3)
6
>>> seq = [1,2,3]
>>> demo(seq)
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() missing 2 required positional arguments: 'b' and 'c'
>>> demo(*seq) #实参序列前加‘*’
6
>>> tup = (1,2,3)
>>> demo(tup)
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() missing 2 required positional arguments: 'b' and 'c'
>>> demo(*tup) #实参序列前加‘*’
6
>>> dic = {1:'a',2:'b',3:'c'}
>>> demo(*dic) #实参序列前加‘*’
6
>>> set = {1,2,3}
>>> demo(*set) #实参序列前加‘*’
6
>>> demo(*dic.values())
abc
>>> def demo(a,b,c):
... print(a,b,c)
...
>>> demo(*(1,2,3)) #调用,序列解包
1 2 3
>>> demo(1,*(2,3)) #位置参数和序列解包同时使用
1 2 3
>>> demo(1,*(2),3)
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() argument after * must be an iterable, not int
>>> demo(1,*(2,),3)
1 2 3
>>> demo(a=1,*(2,3)) #序列解包相当于位置参数,优先处理
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() got multiple values for argument 'a'
>>> demo(b=1,*(2,3))
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() got multiple values for argument 'b'
>>> demo(c=1,*(2,3))
2 3 1
>>> demo(**{'a':1,'b':2},*(3,)) #序列解包不能在关键参数解包之后
File "", line 1
SyntaxError: iterable argument unpacking follows keyword argument unpacking
>>> demo(*(3,),**{'a':1,'b':2})
Traceback (most recent call last):
File "", line 1, in
TypeError: demo() got multiple values for argument 'a'
>>> demo(*(3,),**{'b':1,'c':2})
3 1 2
5.4 return语句
5.5 变量作用域
也可以这么理解:
>>> def demo():
... global x
... x = 3
... y = 4
... print(x,y)
...
>>> x = 5
>>> demo()
3 4
>>> x
3
>>> y #因为在函数外部,函数已经结束了,栈帧已经回收了
Traceback (most recent call last):
File "", line 1, in
NameError: name 'y' is not defined
>>> x = 3
>>> def f():
... print(x) #本意是先输出全局变量X的值,但是不允许这样做
... x = 5 #有赋值操作,因此在整个作用域内x都是局部变量
... print(x)
...
>>> f()
Traceback (most recent call last):
File "", line 1, in
File "", line 2, in f
UnboundLocalError: local variable 'x' referenced before assignment
>>> def demo():
... x = 3 #创建了局部变量,并自动隐藏了同名的全局变量
...
>>> x = 5
>>> x
5
>>> demo() #函数执行,不影响外面全局变量的值
>>> x
5
def scope_test():
def do_local():
spam = "我是局部变量"
def do_nonlocal():
nonlocal spam #这是要求spam必须是已存在的变量
spam = "我不是局部变量,也不是全局变量"
def do_global():
global spam #如果全局作用域内没有spam,就自动新建一个
spam = "我是全局变量"
spam = "原来的值"
do_local()
print("局部变量赋值后:",spam)
do_nonlocal()
print("nonlocal赋值后:",spam)
do_global()
print("全局变量赋值后:",spam)
scope_test()
print("全局变量:",spam)
运行结果:
局部变量赋值后: 原来的值
nonlocal赋值后: 我不是局部变量,也不是全局变量
全局变量赋值后: 我不是局部变量,也不是全局变量
全局变量: 我是全局变量
5.6 lambda表达式
>>> f = lambda x,y,z:x+y+z #可以给lambda表达式起名字
>>> f(1,2,3)
6
>>> g = lambda x,y=2,z=3:x+y+z #参数默认值
>>> g(1)
6
>>> g(2,z=4,y=5) #关键参数
11
>>> L = [(lambda x:x**2),(lambda x:x**3),(lambda x:x**4)]
>>> print(L[0](2),L[1](2),L[2](2))
4 8 16
>>> D = {'f1':(lambda:2+3),'f2':(lambda:2*3),'f3':(lambda:2**3)}
>>> print(D['f1'](),D['f2'](),D['f3']())
5 6 8
>>> L = [1,2,3,4,5]
>>> print(list(map(lambda x:x+10,L))) #模拟向量运算
[11, 12, 13, 14, 15]
>>> L
[1, 2, 3, 4, 5]
>>> def demo(n):
... return n*n
...
>>> demo(5)
25
>>> a_List = [1,2,3,4,5]
>>> list(map(lambda x:demo(x),a_List)) #在lambda表达式中调用函数
[1, 4, 9, 16, 25]
>>> data = list(range(20)) #创建列表
>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> import random
>>> random.shuffle(data) #打乱顺序
>>> data
[16, 10, 5, 15, 13, 4, 8, 12, 0, 6, 19, 7, 1, 2, 18, 3, 14, 11, 17, 9]
>>> data.sort(key=lambda x:x) #和不指定规则效果一样
>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> data.sort(key=lambda x:len(str(x))) #按转换成字符串以后的长度排序
>>> data
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> data.sort(key=lambda x: len(str(x)),reverse=True) #降序排序
>>> data
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]