【问题标题】:Python 2.7 - How to call class method from imported module? “Self” argumentPython 2.7 - 如何从导入的模块中调用类方法? “自我”论证
【发布时间】:2016-11-09 20:33:36
【问题描述】:

我一直在论坛中寻找答案,我发现最相似的是:Python: How to call class method from imported module? “Self” argument issue。但这并没有解决我的问题。

我有 2 个脚本:1-X 和 2-Y。我需要将一些 def () 从 Y 导入到 X,这是我导入和实例化的代码:

X 脚本 - 我有一个名为 txtCorpus 的变量,这是我打算操作的变量

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)

现在是脚本 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 作为参数传递时,我必须以什么方式实例化代码才能工作?

【问题讨论】:

  • from Y import Y-class 这不是有效的语法。请正确复制粘贴您的代码。
  • @AlexHall 我很确定这不是复制和粘贴错误。
  • from Y import Yclass 后面跟txtCorpus 肯定是错的。
  • from Y import PreProcessing 我用这种方式尝试一般地保留它。这不是复制和粘贴错误
  • @AlexHall 我已编辑以免造成进一步的混乱。

标签: python python-2.7 methods import self


【解决方案1】:

您没有包含完整的示例,但您在此处看到的错误 TypeError: initial_processing() takes exactly 1 argument (2 given) 是因为您将 initial_processing 定义为这样:

def initial_processing(self):

只需要 1 个参数(类实例)。然后,向它传递两个参数(instancetxtCorpus):

myCorpus = instance.initial_processing(txtCorpus)

想想这样的类方法:

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

所以你传递了 2 个参数,但只定义了处理 1 的方法。因此,你需要定义它来接受两个参数:

def initial_processing(self, txt):
# code here

【讨论】:

  • 好的。我将def initial_processing(self): 更改为def initial_processing(self,text): 所以我调用了myCorpus = instance.initial_processing(txtCorpus) 方法,我收到了这个错误:return self.__text AttributeError: PreProcessing instance has no attribute '_PreProcessing__text'
【解决方案2】:

错误是定义时需要输入 2 个参数,一个用于 self,一个用于 txtCorput

def initial_processing(self, txt):
  # Operations

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 而不是 PreProcessing ^^)

【讨论】:

    【解决方案3】:

    看起来问题在于您的函数不是您认为的类的一部分。你应该缩进它,使它成为类的一部分,并添加一个参数让它接受(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
    

    【讨论】:

    • 好的。我将def initial_processing(self): 更改为def initial_processing(self,text): 所以我调用了myCorpus = instance.initial_processing(txtCorpus) 方法并收到了这个error: return self.__text AttributeError: PreProcessing instance has no attribute '_PreProcessing__text'
    • @LeandroS.Matos 听起来您从未创建过实例变量__text。尝试在您的构造函数中这样做,例如添加我刚刚编辑答案的__init__ 方法。
    猜你喜欢
    • 2015-09-22
    • 2020-12-25
    • 2013-03-13
    • 2015-04-04
    • 2016-01-03
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多