a=‘abcdef’
for x,y in enumerate(a):
print(x,y)
‘’’
迭代器:
拥有__iter__和__next__方法的对象就是迭代器
‘’’
print(dir([]))
print(dir([].iter()))
整形不能迭代
print('iter’in dir(int))
a=set(dir([].iter()))
b=set(dir())
print(a-b)
列表的长度
print([1,2,3].iter().length_hint())
k=[1,2,3]
d=k.iter()
print(d.next())
print(d.next())
print(d.next())
a=‘1234’
b=list(a)
print(b)
class Fib():
def init(self,num):
self.num=num
self.a=1
self.b=1
self.current=1
def iter(self):
return self
def next(self):
if self.current<=self.num:
temp=self.a
self.a,self.b=self.b,self.a+self.b
self.current+=1
return temp
else:
raise StopIteration
for x in Fib(10):
print(x)
class Fib():
def init(self,num):
self.num=num
self.a=1
self.b=1
self.current=0
def iter(self):
return self
def next(self):
if self.num<=2:
self.current+=1
if self.current==3:
raise StopIteration
return self.a
else:
if self.current<self.num:
ret=self.a
self.a,self.b=self.b,self.a+self.b
self.current+=1
return ret
else:
raise StopIteration
for i in Fib(10):
print(i)
‘’’
闭包:
1.闭包函数必须有内嵌函数
2.内嵌函数必须引用外层函数的变量
3.闭包函数返回内嵌函数的地址(函数名称)
‘’’
def waiceng(b):
a=3
def neiceng(x):
return x*a+b
return neiceng
x=waiceng(9)
print(x(5))
print(x(7))
def waiceng():
b=9
def neiceng():
print(‘哈哈’,b)
print(neiceng.closure)
return neiceng
x=waiceng()
print(x)
面试题
遍历过程中没有调用 所以i值为2 后执行前边
a=[lambda x:x*i for i in range(3)]
print(a0)
print(a1)
print(a2)
def func():
x=4
d1=[]
for i in range(3):
action=lambda x,i=i:x**i
d1.append(action)
return d1
b=func()
print(b0)
print(b1)
print(b2)
i=1
def a(n,i=i):
print(n*i)
i=2
a(3)
‘’’
装饰器:
本质 是一个闭包函数
在不修改原函数及其调用方式的情况下对原函数功能进行拓展
@装饰器名称
‘’’
import time
def decor_shauige(f):
def neibu():
t=time.time()
f()
t2=time.time()
print(‘时间{}’.format(t2-t))
return neibu
@decor_shauige
def func2():
s=0
for i in range(3):
s+=1
print(s)
func2()
def zsq(f):
def nb():
print(’********’)
f()
print(’*******’)
return nb
def zsq2(f):
def nb():
print(’******’)
f()
print(’&&&&&&&&’)
return nb
@zsq2
@zsq
def f1():
print(‘领导来了’)
f1()
print()
from functools import wraps
def acor(f):
def neibu(a,b):
print(’********’)
d=f(a,b)
print(’&&&&&&&&&&’)
return d
return neibu
@acor
def func(a,b):
c=a+b
return c
print(func(2,3))
property装饰器 只能修饰不带参的
class A():
def init(self,name):
self.name=name
@property #属性 把方法当作属性使用
def age(self):
return self.__age
@age.setter
def age(self,age):
if age<0 or age>100:
print(‘不对’)
self.__age=0
else:
self.__age=age
a=A(‘张三’)
a.age=39
print(a.age)
a.age=400
print(a.age)
class A():
def init(self,name):
self.__name=name
@property
def name(self):
return self.__name
# @name.setter
# def name(self,x):
# self.name=x
a=A(‘张三’)
print(a.name)
# a.name=‘李四’
from math import pi
class Cirlce():
def init(self,r):
self.r=r
@property
def zhouchang(self):
return 2piself.r
@property
def area(self):
return piself.rself.r
y=Cirlce(1.5)
print(y.zhouchang)
print(y.area)
#BMI
class A():
def init(self,name,t,g):
self.name=name
self.t=t
self.g=g
@property
def bmi(self):
return self.t/(self.g*self.g)
a=A(‘高’,55,1.63)
print(a.bmi)
生成器
def shengchan(n):
i=1
while i<=n:
yield i #yield 返回值 并不会完全的结束函数 而是保存下来当前的状态 等到下次进去的时候 继续使用
i+=1
x=shengchan(5)
print(x)
for y in x:
print(y)
def hs():
for i in range(3):
yield ‘第%d个包子’%(i+1)
x=hs()
print(next(x))
print(next(x))
print(x.next)
def g():
print(‘a’)
x=yield 10
print(‘接收到数据’,x)
yield x+5
x=g()
print(next(x))
b=x.send(999)
print(b)
def g():
count=1
get_num=0
avg=0
total=0
while True:
get_num=yield avg
total=total+get_num
avg=total/count
count+=1
x=g()
next(x)
print(x.send(10))
print(x.send(20))
print(x.send(30))
#斐波那契
def fib(n):
a,b=1,1
i=1
while i<=n:
yield a
a,b=b,a+b
i+=1
for i in fib(10):
print(i)
def g():
yield from ‘AB’
yield from range(5)
for x in g():
print(x)
def g():
for x in ‘AB’:
yield x
for x in range(3):
yield x
for d in g():
print(d)
a=[i for i in range(5)]
print(a)
b=(i for i in range(5))
print(b)
for x in b :
print(x)
a=(‘鸡蛋%d’%i for i in range(1,11))
print(a)
print(next(a))
print(a.next())
print(list(a))
def generator():
print(‘zzz’)
yield 1
print(‘xxxx’)
yield 2
x=generator()
a=x.next()
print(a)
a=x.next()
print(a)
def ksf():
for i in range(20):
yield ‘康师傅%d’%i
# x=ksf().next()
# print(x)
g=ksf()
for i in g:
print(g)
def g():
print(‘a’)
count=yield 1
print(count)
print(‘b’)
yield 2
a=g()
next(a)
ret2=a.send(‘123’)
print(ret2)