【发布时间】: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