装饰器——闭包
# 装饰器 闭包
'''
如果一个内部函数对外部(非全局)的变量进行了引用,那么内部函数被认为是闭包
闭包 = 函数块 + 定义时的函数环境
'''
def f():
x = 100
y = 200
def mytext():
return x + y
return mytext
s=f()
print(s())
装饰器——高潮1
import time
def 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 time
def 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 time
def 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("-------------")