今天学习了闭包,语法糖(列表推导式),装饰器,迭代器,生成器相关笔记如下
'''
知识点:
1.闭包
2.语法糖(列表推导式)
3.装饰器
4.迭代器
5.生成器
'''
#高阶函数
# def func(a):
# res=a() #func2()
# print(a())
#
# def func2():
# return "python"
#
#
#
# func(func2())
#嵌套函数
# def func1():
# def func2():
# print("hello,world")
# return func2()
#
# func1()
#闭包
def func1(a):
def func2():
print(a+a)
return func2
#func1(2)()
#列表推导式
#三目运算暂时不讲
a=[]
for i in range(10):
a.append(i)
#print(a)
b=[i for i in range(10)] #列表推导式 用的地方很多
#print(b)
#不改变原有的调用方式,但是可以给函数添加新的功能
# def girl():
# return "如此美丽"
#
# print(girl())
# def girl():
# return "小明"+"如此美丽"
#print(girl())
# def modify(func):
# result=func()
# return "小明"+result
#print(modify(girl))
#装饰器(闭包做的)
def modify(func):
def wrapper():
print("开始验证")#当成代码逻辑
result=func()
return "小明"+result
return wrapper
@modify
def girl():
return "如此美丽"
print(girl())
#python 内置装饰器 3个
class Rectangles:
def __init__(self,length,width):
self.length=length
self.width=width
def area(self):
areas=self.length*self.width
return areas
@property#像属性一样访问我们的方法,调用可以不用括号
def areas(self):
areas=self.length*self.width
return areas
@staticmethod #静态方法 和self进行解绑
def func():
print('HELLO,WORLD')
@classmethod #类方法 传递的就是类本身
def show(cls):
print("python")
a=Rectangles(10,5)
#print(a.area())
#print(a.areas)
#a.func()
#Rectangles.func()
#使用类来做装饰器
class TestClass:
def __init__(self,func):
self.func=func
def __call__(self, *args, **kwargs):
print("正在验证")
return self.func
@TestClass
def func_test():
print("qi")
func_test()
#带参数的装饰器
def func1(a):
def func2(func):
def func3(*args,**kwargs):
return a+func(*args)
return func3
return func2
@func1("haha")
def qiyue(c):
return "heihei"+c
print(qiyue("嗯嗯"))
#迭代器 优势是可以优化内存,惰性计算
from itertools import count
li=[1,2,3,4,5,6,]
#print(dir(li))
#__iter__ 代表是一个可迭代的对象
#__next__
'''
['__add__', '__class__', '__contains__', '__delattr__',
'__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__',
'__rmul__', '__setattr__', '__setitem__',
'__sizeof__', '__str__', '__subclasshook__',
'append', 'clear', 'copy', 'count', 'extend',
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
'''#有__iter__没有__next__说明是一个可迭代对象
a=iter(li)
print(dir(a))
'''
['__class__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__length_hint__', '__lt__',
'__ne__', '__new__', '__next__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__setstate__',
'__sizeof__', '__str__', '__subclasshook__']
'''#有了__next__说明是一个迭代器
print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))
# itr=iter(li)
# try:
# while True:
# a=next(itr)
# print(a) #本质是一个for循环
# except StopAsyncIteration as e:
# pass
#生成器
# def func():
# yield 1
# yield 2
# yield 3
# yield 4
#
# b=func()
# print(b.__next__())
# print(next(b))
def cycke(a,n):
count = 0
while True:
if count<n:
count +=1
yield a
else:
break
haha = cycke("七月",8)
print(haha)
for i in haha:
print(i)
标签:__,return,第十天,python,self,学习,func,print,def
From: https://blog.csdn.net/WuMingf_/article/details/143418470