2016-11-09 4 views
0

私は答えを見つけようとフォーラムでここを見てきました。私が見つけた最も類似したものはPython: How to call class method from imported module? “Self” argument issueでした。しかし、それは私の問題を解決しません。Python 2.7 - インポートされたモジュールからクラスメソッドを呼び出す方法"自己"引数

私は2つのスクリプトを持っています:1-Xと2-Y。それは私が

import Y 
from Y import PreProcessing 
txtCorpus 
def status_processing(txtCorpus): 
    instance = PreProcessing() #Here is where to instantiate the class contained within the Y script and where it has some defs that I need 
    myCorpus = instance.initial_processing(txtCorpus) 
#After some other lines of operation ... 
if __name__ == "__main__": 
    status_processing(txtCorpus) 
を操作しようとするものです txtCorpusという変数を持っている -

Xスクリプト:私はXにYからいくつかのDEFを()をインポートする必要があり、ここで私は、インポートおよびインスタンス化私のコードです私はそれがある方法を実行すると

は今スクリプトY

class PreProcessing(): 
    @property 
    def text(self): 
     return self.__text 

    @text.setter 
    def text(self, text): 
     self.__text = text 

tokens = None 
def initial_processing(self): 
#Operations 

、次のエラーが表示されます。

TypeError: initial_processing() takes exactly 1 argument (2 given) 

私はこれmyCorpus = instance.initial_processing()を行うと、次のエラーが表示されます。

AttributeError: PreProcessing instance has no attribute '_PreProcessing__text' 

私は、パラメータとしてtxtCorpusを渡す際に動作するコードをインスタンス化する必要があり方法は何ですか?

+0

'from Y import Y-class'は有効な構文ではありません。あなたのコードを適切に貼り付けてください。 –

+0

@AlexHall私はそれがコピー&ペーストエラーではないことを確信しています。 –

+0

'from Y import Yclass'の後ろに' txtCorpus'という行があるのは間違いです。 –

答えて

0

あなたは完全なexamplが含まれていませんが、あなたのようなinitial_processingを定義するので、あなたがここにTypeError: initial_processing() takes exactly 1 argument (2 given)を見ているエラーは、次のとおりです。

def initial_processing(self): 

のみ1つの引数(クラスのインスタンス)をとります。ですから、2つの引数を渡すだけ1を処理するメソッドを定義

instance.initial_processing(txtCorpus) 
# equivalent to 
initial_processing(instance, txtCorpus) 

:このようなクラスメソッドの

myCorpus = instance.initial_processing(txtCorpus) 

は思う:その後、あなたはそれ二つの引数(instance、およびtxtCorpus)を渡します。したがって、2つの引数を取るように定義する必要があります:

def initial_processing(self, txt): 
# code here 
+0

私はメソッド 'myCorpus = instance.initial_processing(txtCorpus)'を呼び出し、 私はこのエラーを受け取りました: 'return self .__ text(self、text):def self.__ text AttributeError:前処理インスタンスに属性 '_PreProcessing__text'がありません ' –

0

あなたの関数は、あなたが考えるクラスの一部ではないように見えます。クラスの一部であるようにインデントし、それを受け入れる引数を追加します(txtCorpus)。

class PreProcessing(): 
    def __init__(self): 
     self.__text = None 

    @property 
    def text(self): 
     return self.__text 

    @text.setter 
    def text(self, text): 
     self.__text = text 

    tokens = None # Not sure what this is for...? 
    def initial_processing(self, txt): 
      #Operations 
+0

私は 'myCorpus = instance.initial_processing(txtCorpus)'メソッドを呼び出し、このエラーを受け取りました:return self .__ text AttributeError:前処理インスタンスに属性 '_PreProcessing__text''がありません –

+0

@ LeandroS.Matosインスタンス変数 '__text'を作成したことがないようです。私の答えを編集したばかりの '__init__'メソッドを追加するなど、コンストラクタでそうしてください。 – user108471

0

エラーが定義する際に2つの引数、

def initial_processing(self, txt): 
    # Operations 

txtCorput

のための自己のために1つずつに配置する必要があるということであるYファイル:

class PreProcessing(): 
    @property 
    def text(self): 
     return self.__text 

    @text.setter 
    def text(self, text): 
     self.__text = text 

    tokens = None 
    def initial_processing(self, corpus): 
    # Operations 

と間違って何もありませんあなたのXスクリプト(Yclassをインポートしていないことを前処理^^)

関連する問題