Get startedGet started for free

Writing a class for your package

We've covered how classes can be written in Python. In this exercise, you'll be creating the beginnings of a Document class that will be a foundation for text analysis in your package. Once the class is written you will modify your package's __init__.py file to make it easily accessible by your users.

Below is the structure of where you'll be working.

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

This exercise is part of the course

Software Engineering Principles in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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
Edit and Run Code