马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
中秋过后,学习归来!函数的学习有点玄乎玄乎的了P42-P49 ######################################P46
import time
def func():
x=520
print(x)
func()
def funa():
x=880
def funb():
x=550
print(x)
funb()
funa()
def funa():
x=880
def funb():
print(x)
return funb
funa()()
fun=funa()
fun()
def power(exp):
def exp_of(base):
return base ** exp
return exp_of
square = power(2)
print(square(2))
cube = power(3)
print(cube(3))
def outer():
x=0
y=0
def inner(x1,y1):
nonlocal x,y
x=x1+x1
y=y1+y1
print(f'现在,x={x},y={y}')
return inner
move=outer()
move(1,2)
######################################P47
def myfun():
print('调用函数')
def report(func):
print('开始')
func()
print('结束')
report(myfun)
def time_master(func):
print('开始运行')
start =time.time()
func()
end = time.time()
time_=(end-start)
print('结束运行')
print(f'总共运行:{time_:.2f}秒')
def myfunc():
time.sleep(2)
print('Hello')
time_master(myfunc)
###############################
def time_master(func):
def call_func():
print('开始运行')
start = time.time()
func()
end = time.time()
time_ = (end - start)
print('结束运行')
print(f'总共运行:{time_:.2f}秒')
return call_func
@time_master
def myfunc():
time.sleep(2)
print('Hello')
myfunc()
#######################################
def call_func(func):
print('开始运行')
start = time.time()
func()
end = time.time()
time_ = (end - start)
print('结束运行')
print(f'总共运行:{time_:.2f}秒')
return call_func
def myfunc():
time.sleep(2)
print('Hello')
aa=myfunc()
#######################################
def logger(msg):
def time_master(func):
def call_func():
print('开始运行')
start = time.time()
func()
end = time.time()
time_ = (end - start)
print('结束运行')
print(f'总共运行:{time_:.2f}秒')
return call_func
return time_master
@logger(msg='a')
def funa():
time.sleep(1)
print('正在使用funa')
@logger(msg='b')
def funb():
time.sleep(1)
print('正在使用funb')
funa()
funb()
#######################################P47
def logger(msg):
def time_master(func):
def call_func():
print('开始运行')
start = time.time()
func()
end = time.time()
time_ = (end - start)
print('结束运行')
print(f'总共运行:{time_:.2f}秒')
return call_func
return time_master
def funa():
time.sleep(1)
print('正在使用funa')
def funb():
time.sleep(1)
print('正在使用funb')
funaa=logger(msg='a')(funa)
funbb=logger(msg='b')(funb)
funa()
funb()
#######################################P48
def squarex(x):
return x*x
x1=square(3)
print(x1)
squarey= lambda y:y*y
print(squarey(5))
y=[lambda x:x*x,2,3]
print(y[0](y[1]))
print(y[0](y[2]))
mapp=map(lambda x:ord(x)+10,'FishC')
print(list(mapp))
def boring(x):
return ord(x)+10
mappp=list(map(boring,'FishC'))
print(mappp)
li=list(filter(lambda x:x%2, range(10)))
print(li)
#######################################P49-生成器
def counter():
i=0
while i<=5:
yield i
print(i)
i=i+1
c=counter()
print(c)
def fib():
back1=0
back2=1
while True:
yield back1
back1,back2=back2,back1+back2
# back1=back2
# back2=back1+back2
f=fib()
print(next(f))
print(next(f))
print(next(f))
print(next(f))
print(next(f))
print(next(f))
t=(i ** 2 for i in range(10))
print(t)
在生成器的学习中,斐波那契数列, back1,back2=back2,back1+back2如果写成
back1=back2
back2=back1+back2
结果居然不一样,很是神奇!
|