अपने पैकेज के लिए एक क्लास लिखना
हमने देखा कि Python में क्लासेज़ कैसे लिखी जाती हैं। इस अभ्यास में, आप अपने पैकेज में टेक्स्ट विश्लेषण की बुनियाद बनने वाली Document क्लास की शुरुआत बनाएँगे। क्लास लिख लेने के बाद आप अपने पैकेज की __init__.py फ़ाइल को संशोधित करेंगे ताकि यह आपके यूज़र्स के लिए आसानी से उपलब्ध हो जाए।
नीचे वह स्ट्रक्चर दिया है जहाँ आप काम करेंगे।
working_dir
├── text_analyzer
│ ├── __init__.py
│ ├── counter_utils.py
│ ├── document.py
└── my_script.py
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Software Engineering Principles
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# 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