#装饰器1
def doubler(func):
def wrapper():
return func() * 2
return wrapper
# def get_five():
# return 5
# doubled = doubler(get_five)
# print(doubled())
@doubler
def get_x():
return 10
print("装饰器调用结果:")
print(get_x())
#装饰器2
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
def say_hello():
print("Hello!")
say_hello = my_decorator(say_hello)
say_hello()
#--------------函数推导器-----------
print("函数推导器")
fruits = ['Banana', 'Apple', 'Lime']
loud_fruits = [fruit.upper() for fruit in fruits]
print(loud_fruits)
fruits = ['Banana', 'Apple', 'Lime']
print(list(enumerate(fruits)))
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
#函数生成器
def my_range(n):
i = 0
while i < n+8:
yield i*i
i +=1
for x in my_range(5):
print(x)
输出的结果:
0
1
4
9
16
25
36
49
64
81
100
121
144