def 函数名: 函数体 [return 返回值]
pythondef add(*args):
print(args) #参数收集为一个元组
print(*args) #参数解构
print(sum(args))
add(2,3,45)
def add_dict(**kwargs):
print(kwargs)
print(kwargs['x'])
add_dict(x=1,zz=11)
(2, 3, 45) 2 3 45 50 {'x': 1, 'zz': 11} 1
参数定义顺序为:
pythondef fn(x,y,z=3,*args,m=4,n,**kwargs):
print(x,y,z,m,n)
print(args)
print(kwargs)
fn(1,223,33,44,5,m=22,n=11,xs= 'dsa')
1 223 33 22 11 (44, 5) {'xs': 'dsa'}
pythondef add(x,y):
return x+y
#解构操作,可迭代对象(线性结构), *可以看做是迷你型的for i in 形式
print(add(*[1,4]))
print(add(*(1,33)))
print(add(*range(1,3)))
print(add(**{'x':1,"y":1111})) #字典需要**
def add(*iter):
return sum(iter)
print(add(*[1,2,3,4,5]))
5 34 3 1112 15
作用:结束函数调用,返回函数结果
pythondef show():
return 1,3,4
print(show() )
# 默认封装为一个元组返回
x,y,z = show()
print(x,y,z) #解构
(1, 3, 4) 1 3 4
全局作用域: 整个程序运行环境可见
局部作用域: 在函数、类的内部可见 局部变量使用的范围不能超过其所在的局部作用域
pythonx = 5 #全局变量
def foo():
print(x) #可以读取
#x += 1 #重新定义x,UnboundLocalError
foo()
#foo函数内部可以读到x,但是不能修改它。
5
python# global 关键字将函数体内的变量提升为全局,,慎用。
x = 5 #全局变量
def foo():
global x
print(x) #可以读取
x += 1
foo()
5
自由变量:未在本地作用域中定义的变量 闭包:内层函数引用到了外层函数自由变量的操作
pythondef counter():
c = [0]
def inc():
c[0] += 1
return c[0]
return inc
foo = counter()
print(foo(),foo())
c = 100
print(foo())
1 2 3
nonlocal关键字可以将变量标记为不在本地作用域定义,而在上一级作用域局中定义,但是不能为全局作用域。
pythondef counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
foo= counter()
print(foo(),foo())
1 2
del 对象
python#最大深度获取
import sys
sys.getrecursionlimit()
3000
打印斐波那契数列
pythondef fib(n):
return 1 if n < 2 else fib(n-1) + fib(n-2)
for i in range(10):
print(fib(i),end=' ')
1 1 2 3 5 8 13 21 34 55
字典扁平化,实现效果如下:
源字典 {'a':{'b':1,'c':2},'d':{'e':3,'f':{g:4}}} 目标字典 {'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}
pythonsource = {'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}}
target={}
def flatmap(src,pre=''):
for k,v in src.items():
if isinstance(v,(list,tuple,set,dict)):
flatmap(v,pre=pre+k+'.')
else:
target[pre+k] = v
flatmap(source)
print(target)
{'a.b': 1, 'a.c': 2, 'd.e': 3, 'd.f.g': 4}
将一个数逆序分别放入列表中,例如1234 => [4,3,2,1],使用递归。
pythondata = str(1234)
def revert(x):
if x == -1:
return ''
return data[x] + revert(x-1)
print(revert(3))
4321
if 的一行表达式 a if b else c
如果满足b则执行a,否则执行c
pythona = 6
3 if a> 3 else 4
3
pythona = 2
3 if a> 3 else 4
4
形式: (lambda 参数列表:表达式)(传入参数)
匿名函数一般当做其他高阶函数的参数传入,可以实现函数的高级功能。
pythonprint((lambda x:x**2)(3))
print((lambda x:x)(33))
9 33
python[x for x in (lambda *args: map(lambda x: (x+1,args), args))(*range(5))]
[(1, (0, 1, 2, 3, 4)), (2, (0, 1, 2, 3, 4)), (3, (0, 1, 2, 3, 4)), (4, (0, 1, 2, 3, 4)), (5, (0, 1, 2, 3, 4))]
pythonlist(map(lambda x: x + x, [1, 3, 5, 7, 9]))
[2, 6, 10, 14, 18]
生成器是可以有生成器表达式得到的一种对象,也可以使用yield关键字生成 生成器表达式? 把列表解析器的[]换成() 就成为了生成器表达式,可以使用next()逐个取值。 或者 使用yield(iterable)
使用next()关键字取值,超界会报错。
生成器的好处: 延迟计算,惰性求职
pythondef counter():
i = 0
while True:
i += 1
yield i
def inc(c):
return next(c)
xx = counter()
print(next(xx))
print(next(xx))
print(next(xx))
print(next(xx))
print(next(xx))
1 2 3 4 5
yield from
pythondef inc():
for x in range(10):
yield x
foo = inc()
print(next(foo))
print(next(foo))
print(next(foo))
#可以改写为
def inc():
yield from ['bbq',1,'aax',23]
foo = inc()
print(next(foo))
print(next(foo))
print(next(foo))
0 1 2 bbq 1 aax
python
1
python
python
python
python
本文作者:mykernel
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!