2017-09-27 12 views
0

私はPythonにはかなり新しく、スーパークラスを継承しようとしています。Pythonの継承。属性を読み取ることができません

スーパークラスは、次のようになります。

class LanguageModel: 
    # Initialize and train the model (ie, estimate the model's underlying probability 
    # distribution from the training corpus) 
    def __init__(self, corpus): 
     print("""Your task is to implement three kinds of n-gram language models: 


    #enddef 

    # Generate a sentence by drawing words according to the 
    # model's probability distribution 
    # Note: think about how to set the length of the sentence 
    # in a principled way 
    def generateSentence(self): 
     print("Implement the generateSentence method in each subclass") 
     return "mary had a little lamb ." 
    #emddef 

そして、ここで私が最初に上記の操作を実行して、コーパスの総語数を取得しようとしましたサブクラス

class UnigramModel(LanguageModel): 
    def __init__(self, corpus): 
     print("Subtask: implement the unsmoothed unigram language model") 
    #endddef 

    def generateSentence(self): 
     # Gets the total number of words in the corpus 
     wordCount = 0 
     for sentence in self.corpus: 
      for word in sentence: 
       wordCount += 1 
     print(wordCount) 

ですが、それは私にエラーを与えますその関数を呼び出そうとすると、 "UnigramModel 'オブジェクトに属性'コーパス 'がありません。

+1

'def __init __(self、corpus)の後:' add 'self.corpus = corpus'です。 –

答えて

1

上記のようにコーパスを宣言するか、getter-setter methプライベート属性のods。

DEF INIT(自己、コーパス): self.corpus =コーパス @property DEFコーパス(自己): 戻り自己.__コーパス @setter DEFコーパス(自己、値): self .__ corpus = value

関連する問題