#生成器函数:函数体内包含有yield关键字,该函数执行的结果是生成器
def foo():
print('first------>')
yield 1
print('second----->')
yield 2
print('third----->')
yield 3
print('fouth----->')
g=foo()
# print(g)
# from collections import Iterator
# print(isinstance(g,Iterator))
#生成器就是迭代器
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# for i in g: #obj=g.__iter__() #obj.__next__()
# print(i)
'''
yield的功能:
1.与return类似,都可以返回值,但不一样的地方在于yield返回多次值,而return只能返回一次值
2.为函数封装好了__iter__和__next__方法,把函数的执行结果做成了迭代器
3.遵循迭代器的取值方式obj.__next__(),触发的函数的执行,函数暂停与再继续的状态都是由yield保存的
'''
def countdown(n):
print('starting countdown')
while n > 0:
yield n
n-=1
print('stop countdown')
g=countdown(5)
# print(g)
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
# print(g.__next__())
#
# for i in g:
# print(i)
#tail -f a.txt
import time
def tail(filepath,encoding='utf-8'):
with open(filepath,encoding=encoding) as f:
f.seek(0,2)
while True:
# f.seek(0, 2) #不行
line=f.readline()
if line:
# print(line,end='')
yield line
else:
time.sleep(0.5)
g=tail('a.txt')
print(g)
print(g.__next__())
#
# for i in g:
# print(i)
#tail -f a.txt | grep 'error'
def grep(lines,pattern):
for line in lines:
if pattern in line:
# print(line)
yield line
# tail_g=tail('a.txt')
# print(g)
# grep_g=grep(tail_g,'error')
# print(grep_g)
#
# print(grep_g.__next__())
# for i in grep_g:
# print(i)
#tail -f a.txt |grep 'error' |grep '404'
# g1=tail('a.txt')
#
# g2=grep(g1,'error')
#
# g3=grep(g2,'404')
# for i in g3:
#print(i)
#生成器函数补充
def countdown(n):
while n > 0:
yield n
n-=1
# g=countdown(5)
# print(g.__next__())
# print(g.__next__())
#
# print('='*20)
# for i in g:
# print(i)
#
# print('*'*20)
# for i in g:
# print(i)
# for i in countdown(5):
# print(i)
# print('*'*20)
# for i in countdown(5):
# print(i)
# print('*'*20)
# for i in countdown(5):
# print(i)
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5).__next__())
# print(countdown(5),countdown(5),countdown(5))
三元表达式:
x=2
y=3
# if x > y:
# print(x)
# else:
# print(y)
# res='aaaaa' if x > y else 'bbbbbbb'
#
# print(res)
# def max2(x,y):
# # if x > y:
# # return x
# # else:
# # return y
#
# return x if x > y else y
#
# print(max2(1,2))
列表解析:
s='hello'
# l=[]
# for i in s:
# res=i.upper()
# l.append(res)
#
# print(l)
# l=[]
# for i in range(10000):
# l.append(i)
# print(l)
# l=[1,2,3,4]
# l_new=[]
# for i in l:
# res=i**2
# l_new.append(res)
# print(l_new)
#列表解析:
# s='hello'
# res=[i.upper() for i in s]
# print(res)
# l=[1,31,73,84,57,22]
# l_new=[]
# for i in l:
# if i > 50:
# l_new.append(i)
# print(l_new)
# res=[i for i in l if i > 50]
# print(res)
# for i in obj1:
# if 条件1:
# for i in obj2:
# if 条件2:
# for i in obj3:
# if 条件3:
# ...
# l=[1,31,73,84,57,22]
# # print([i for i in l if i > 50])
# # print([i for i in l if i < 50])
# print([i for i in l if i > 20 and i < 50])
#生成器表达式
# [i for i in range(1000000000000000000000000000000000000000000)]
g=(i for i in range(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000))
print(g)
print(next(g)) #next(g) == g.__next__()
print(next(g)) #next(g) == g.__next__()
print(next(g)) #next(g) == g.__next__()
print(next(g)) #next(g) == g.__next__()
print(next(g)) #next(g) == g.__next__()
#len('hello') 'hello'.__len__()
# print(len('hello'))
# print('hello'.__len__())
# iter(g) #g.__iter__()