<分区>
如果引发异常,是否有任何方法可以继续执行 try
block ?我认为答案是否定的,但我认为下面的代码很难看。
def preprocess(self, text):
try:
text = self.parser(text)
except AttributeError:
pass
try:
terms = [term for term in text if term not in self.stopwords]
text = list_to_string(terms)
except AttributeError:
pass
try:
terms = [self.stemmer.stem(term) for term in text]
text = list_to_string(terms)
except AttributeError:
pass
return text
还有另一种方法可以用 pythonic 形式做到这一点吗?