装饰器——闭包 # 装饰器 闭包 ''' 如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包 闭包 = 函数块 + 定义时的函数环境 ''' def f(): x = 100 y = 200 def myte
装饰器——闭包
# 装饰器 闭包'''
如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包
闭包 = 函数块 + 定义时的函数环境
'''
def f():
x = 100
y = 200
def mytext():
return x + y
return mytext
s=f()
print(s())
装饰器——高潮1
import timedef foo():
print("foo..........")
time.sleep(2)
def root():
print("root---------")
time.sleep(1)
def get_time(h):
start = time.time()
h
stop = time.time()
print("运行时间为: %s" % (stop - start))
get_time(root())
装饰器——高潮2
import timedef get_time(h):
def inner():
start = time.time()
h()
stop = time.time()
print("运行时间为: %s" % (stop - start))
return inner
@get_time
def foo():
print("foo..........")
time.sleep(2)
@get_time
def root():
print("root---------")
time.sleep(1)
foo()
装饰器——函数功能添加参数
import timedef get_time(f):
def inner(*s):
start = time.time()
f(*s)
end = time.time()
print("使用时长为: %s" % (end - start))
return inner
@get_time
def add(*a):
sum = 0
for i in a:
sum += i;
print(sum)
time.sleep(2)
add(2, 3, 5, 7)
列表生成式
# 列表生成式a = [x * x for x in range(10)]
print(a)
列表生成器
# 列表生成器s = (x * 2 for x in range(5))
# print(s)
# print(s.__next__())
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print("*-*--**--*--*-*-*-*---*")
def foo():
print("ok1")
yield 1
print("ok2")
yield 2
g = foo()
# next(g)
# next(g)
for i in g:
# while True:
# i = next(foo())
print(i)
print("/*//***/*/***//*/**/*")
a = [1, 2, 3]
斐波那契数列
def fib(m):i, a, b = 0, 0, 1
while i < m:
print(b)
a, b = b, a + b
i += 1
# fib(10)
z = 10
c = 20
print("------------")
z, c = c, z + c
print(z)
print(c)
print("*************")
z = 10
c = 20
z = c
c = z + c
print(z)
print(c)
print("-------------")
【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!
【重要说明】本文为本人的学习记录,论点和观点仅代表个人而不代表当时技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【Gitee地址】秦浩铖:https://gitee.com/wjw1014