我编写了一个类,它从文件中读取数据,并使其可用于以更大程序的其他部分的可用形式进行操作。
我将以下代码作为类定义的一部分(为简洁起见,省略了其余的类定义):
from itertools import tee
class ParsedFile():
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.file = open(self.filename)
return self
def __exit__(self, cls, err, tb):
self.file.close()
if err is not None:
return False
def __iter__(self):
try:
# spin off a new iterator for yielding
# so the existing one isn't consumed
self._ifile, i = tee.(self._ifile)
except AttributeError:
# two iterators: one to be stored
# and spin out a new one for yielding
self._ifile, i = tee.(iter(self._file))
# yield results from new iterator
yield from i
def __len__(self):
try:
# assume length has already been calculated
return self._len
except AttributeError:
# need to calculate length for the first time
for ind,_ in enumerate(self):
pass
self._len = ind + 1
# This line violates DRY:
return self._len
这似乎运作良好。
但是,我的__iter__
方法违反了DRY原则。我并不过分担心和#34; pythonic"将此视为我的设计可能存在缺陷/可以改进的信号。我能想出的唯一选择是添加while
循环。
def __len__(self):
while True:
try:
# assume length has already been calculated
return self._len
except AttributeError:
# need to calculate length for the first time
for ind,_ in enumerate(self):
pass
self._len = ind + 1
我担心以这种方式这样做可能导致不可预见的错误,而且只是感觉马虎。有没有更好的方法可以重做工作以避免重复?是否有充分的理由避免上述while
循环解决方案?