From 8c8441b3795d57161baf49f9163e5f25ec7f684b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=9E=E5=A7=AC?= <1252984281@qq.com> Date: Wed, 24 Mar 2021 13:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E8=8A=82=E4=BD=9C=E4=B8=9A=20=E6=B7=BB=E5=8A=A0=EF=BC=9A?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E7=AC=AC=E4=B8=89=E8=8A=82=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lesson3-3.py" | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\344\270\211\345\221\250/lesson3-3.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\344\270\211\345\221\250/lesson3-3.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\344\270\211\345\221\250/lesson3-3.py" new file mode 100644 index 00000000..a6562e68 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\344\270\211\345\221\250/lesson3-3.py" @@ -0,0 +1,89 @@ + +# 练习作用域之间的转换 +# global + +x = 'yuji' +def call(): + global x + x = 'zhenji' + print(x) +call() + +# nonlocal + +def ruler(): + num_a = 10 + num_b = 5 + def multi(num): + nonlocal num_a,num_b + num_a += 5 + num_b += num + return num_a * num_b + return multi + +new_ruler = ruler() +print(new_ruler(1)) + +# local +num_c = 11 # 全局变量 +def local_gover(): + num_d = 12 # 局部变量 + print(num_d) + +local_gover() +print(num_c) + + + + +# 默写一个装饰器, 用来输出函数的执行时间. +import time +def clockout(x): + def timenote(*args,**kwargs): + start_time = time.time() + result = x(*args,**kwargs) + end_time = time.time() + print(f"{x.__name__} sum time:{format(end_time - start_time,'.2f')}s") + return result + return timenote + +@clockout +def havefun(num_a,num_b): + count = 1 + while True: + if count > num_a * num_b: + break + count += 5 + +havefun(5,1) + + +# 使用装饰器来为斐波那契函数添加缓存 + +def cache_deco(f): + what = {} + def wrapper(*args): + if args not in what: + what[args] = f(*args) + return what[args] + + return wrapper + +@cache_deco +@clockout +def Fibo(n): + if n <= 1: + return n + else: + return Fibo(n-1) + Fibo(n-2) + +f = int(input('请输入项数:')) + +if f <= 0: + print('请重新输入正数:') + +else: + print('斐波那契数列为:') + + for i in range(f): + print(Fibo(i)) -- Gitee