시작하기무료로 시작하기

패키지를 위한 클래스 작성하기

이전 단계에서 Python에서 클래스를 작성하는 방법을 살펴봤습니다. 이번 연습에서는 패키지에서 텍스트 분석의 기반이 될 Document 클래스를 처음부터 만들어 보겠습니다. 클래스를 작성한 뒤에는, 사용자가 쉽게 사용할 수 있도록 패키지의 __init__.py 파일을 수정할 거예요.

아래는 작업할 디렉터리 구조입니다.

working_dir
├── text_analyzer
│    ├── __init__.py
│    ├── counter_utils.py
│    ├── document.py
└── my_script.py

이 연습은 강의의 일부입니다

Python으로 배우는 소프트웨어 공학 원칙

강의 보기

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Define Document class
class Document:
    """A class for text analysis
    
    :param text: string of text to be analyzed
    :ivar text: string of text to be analyzed; set by `text` parameter
    """
    # Method to create a new instance of Document
    def ____(____, text):
        # Store text parameter to the text attribute
        ____.text = text
코드 편집 및 실행