為你的套件撰寫一個類別
我們已經介紹過如何在 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