"""
Sentence 类第1版:单词序列
author:daihaolong
2019年11月18日
v1
"""
import re, reprlib
RE_WORD = re.compile(‘\w+‘)
class Sentence:
def __init__(self, text):
self.text = text
self.word = RE_WORD.findall(text)
def __getitem__(self, item):
return self.word[item]
def __repr__(self):
return "Sentence(%s)" % reprlib.repr(self.text)
s = Sentence(‘"The time has come," the Walrus said,‘)
print(s)
for word in s: # 为什么s 可迭代?因为它实现了__getitem__ 方法
print(word)
print(list(s))
# **********************************************************运行结果
Sentence(‘"The time ha... Walrus said,‘)
The
time
has
come
the
Walrus
said
[‘The‘, ‘time‘, ‘has‘, ‘come‘, ‘the‘, ‘Walrus‘, ‘said‘]
# 任何 Python 序列都可迭代的原因是,它们都实现了 __getitem__ 方法。其实,标准的序
# 列也都实现了 __iter__ 方法,因此你也应该这么做。之所以对 __getitem__ 方法做特殊处
# 理,是为了向后兼容,而未来可能不会再这么做
"""
Sentence 类第2版:
author:daihaolong
2019年11月18日
v2
典型的迭代器,需要实现__iter__和__next__方法
"""
import re, reprlib
RE_WORD = re.compile("\w+")
class Sentence:
def __init__(self, text):
self.text = text
self.words = RE_WORD.findall(text)
def __iter__(self):
return SentenceIterator(self.words)
def __repr__(self):
return "Sentence(%s)" % reprlib.repr(self.text)
class SentenceIterator:
def __init__(self, words):
self.index = 0
self.words = words
def __iter__(self):
return self
def __next__(self):
try:
word = self.words[self.index]
except IndexError:
raise StopIteration()
self.index += 1
return word
s = Sentence(‘"The time has come," the Walrus said,‘)
print(s)
for word in s: # 为什么s 可迭代?因为它实现了__getitem__ 方法
print(word)
print(list(s))
"""
Sentence 类第3版:
author:daihaolong
2019年11月18日
v3
典型的迭代器,需要实现__iter__和__next__方法
"""
import re, reprlib
RE_WORD = re.compile("\w+")
class Sentence:
def __init__(self, text):
self.text = text
self.words = RE_WORD.findall(text)
def __iter__(self):
for word in self.words:
yield word
return
def __repr__(self):
return "Sentence(%s)" % reprlib.repr(self.text)
s = Sentence(‘"The time has come," the Walrus said,‘)
print(s)
for word1 in s: # 为什么s 可迭代?因为它实现了__getitem__ 方法
print(word1)
print(list(s))
"""
Sentence 类第4版:
author:daihaolong
2019年11月18日
v4
使用惰性版本
"""
import re, reprlib
RE_WORD = re.compile(‘\w+‘)
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return "Sentence(%s)" % reprlib.repr(self.text)
def __iter__(self):
for word in RE_WORD.finditer(self.text): # 这样就是惰性的,不用一下生产一个列表
yield word.group()
s = Sentence(‘"The time has come," the Walrus said,‘)
print(s)
for word1 in s: # 为什么s 可迭代?因为它实现了__getitem__ 方法
print(word1)
print(list(s))
"""
Sentence 类第5版:
author:daihaolong
2019年11月18日
v5
使用惰性版本
"""
import re, reprlib
RE_WORD = re.compile(‘\w+‘)
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return "Sentence(%s)" % reprlib.repr(self.text)
def __iter__(self): # 直接使用列表生成式,带括号的其实就是生成器
return (word.group() for word in RE_WORD.finditer(self.text))
s = Sentence(‘"The time has come," the Walrus said,‘)
print(s)
for word1 in s: # 为什么s 可迭代?因为它实现了__getitem__ 方法
print(word1)
print(list(s))
原文:https://www.cnblogs.com/dadaizi/p/11964749.html