Python 构造函数及其使用方法
我创建了以下构造函数:Python 构造函数及其使用方法,python,methods,Python,Methods,我创建了以下构造函数: class Analysis: def __init__(self, file_list, tot_col, tot_rows): self.file_list = file_list self.tot_col = tot_col self.tot_rows = tot_rows 然后,我从同一个文件中获得了方法full\u analysis()callcalc\u total\u rows(): def ful
class Analysis:
def __init__(self, file_list, tot_col, tot_rows):
self.file_list = file_list
self.tot_col = tot_col
self.tot_rows = tot_rows
然后,我从同一个文件中获得了方法full\u analysis()
callcalc\u total\u rows()
:
def full_analysis(self):
"""Currently runs all the analysis methods"""
print('Analysing file...\n' +
'----------------------------\n')
calc_total_rows()
从另一个文件中,我调用了full\u analysis()
,但是出现了错误,称未定义calc\u total\u rows()
,方法就在它下面。
我对Python缺乏经验,但是我试图重新安排代码,并在不同的地方添加“self”,但没有效果
另一个文件确实满足构造函数的要求,如果我删除
calc\u total\u rows()
方法,打印行将运行。但是,我不希望单独调用每个方法,而是希望调用一个运行所有方法的方法 我要是早点找到这个就好了
为了解决这个问题,我不得不在方法前面使用self
在我的例子中:
def full_analysis(self):
"""Currently runs all the analysis methods"""
print('Analysing file...\n' +
'----------------------------\n')
self.calc_total_rows()
这是有效的。如果
calc\u total\u rows
是您的问题所暗示的一个实例方法,那么您需要从full\u analysis
中调用self.calc\u total\u rows()
。与其他一些语言不同,Python在方法范围内没有隐式实例引用;必须从self
显式检索成员方法