开始使用免费开始使用

为您的包编写一个类

我们已经讲过如何在 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
编辑并运行代码