我有一个类,我传递一个文档列表,在一个方法中,它创建了这些文档的列表:
class Copy(object):
def __init__(self, files_to_copy):
self.files_to_copy = files_to_copy
这里,它创建了一个文件列表:
def create_list_of_files(self):
mylist = []
with open(self.files_to_copy) as stream:
for line in stream:
mylist.append(line.strip())
return mylist
现在,我尝试在类中的另一个方法中访问该方法:
def copy_files(self):
t = create_list_of_files()
for i in t:
print i
然后我在if __name__ == "__main__"
下运行以下内容:
a = Copy()
a.copy_files()
这引发:
TypeError: create_list_of_files() takes exactly 1 argument (0 given)
我使用的方法错了吗?
答案 0 :(得分:1)
你需要关闭self
方法,这是" 1参数"该方法正在寻找
t = self.create_list_of_files()
答案 1 :(得分:0)
您需要按以下方式致电create_list_of_files
:
self.create_list_of_files()
答案 2 :(得分:0)
您没有将任何变量传递给该类。在init方法中,代码声明 init 接受一个变量files_to_copy。您需要传递存储正确信息的变量。例如:
class Copy(object):
def __init__(self, files_to_copy):
self.files_to_copy = files_to_copy
#need to pass something like this:
a = Copy(the_specific_variable)
#now, can access the methods in the class