2.4.1 첫 번째 방법, 클래스 사용하기
저는 함수형 프로그래밍 방식을 선호하지만, 그럼에도 불구하고 클래스는 상태 기능을 캡슐화하는 훌륭한 방법입니다.
클래스 기반으로 상태를 유지하며 단어 읽기
>>> class Wordlist:
... def __init__(self, initial=[]):
... self._words = initial
...
... def add_words(self, fname):
... self._words.extend(Path(fname).read_text().split())
...
... def reset(self, initial=None):
... self._words = initial if initial is not None else []
...
... def __repr__(self):
... return str(self._words)
...