एक non-public मेथड लिखना
पाठ में, आपने देखा कि non-public मेथड्स के जरिए classes में नई functionality कैसे जोड़ते हैं. किसी मेथड को non-public बनाकर आप यूज़र को संकेत देते हैं कि यह मेथड सिर्फ पैकेज के अंदर ही इस्तेमाल किया जाए.
इस अभ्यास में, आप एक non-public मेथड परिभाषित करेंगे, जिसे आपकी class शब्दों की गिनती करने के लिए इस्तेमाल करेगी.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Software Engineering Principles
अभ्यास निर्देश
collectionsसेCounterऔर फंक्शनtokenize()आपके environment में लोड कर दिए गए हैं.count_wordsनाम का एक मेथड जोड़िए और उसे non-public मेथड बनाइए.- अपने non-public मेथड को यह functionality दीजिए कि वह
Counter()का उपयोग करtokensattribute की सामग्री को गिने. - अपने नए फंक्शन का उपयोग
__init__मेथड में कीजिए.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
class Document:
def __init__(self, text):
self.text = text
# pre tokenize the document with non-public tokenize method
self.tokens = self._tokenize()
# pre tokenize the document with non-public count_words
self.word_counts = self.____()
def _tokenize(self):
return tokenize(self.text)
# non-public method to tally document's word counts with Counter
def ____(self):
return ____(____.tokens)