Python 2.7 我如何与self传递参数

Python 2.7 我如何与self传递参数,python-2.7,Python 2.7,当尝试运行此程序时,错误作为两个必需参数抛出,self需要作为其中一个参数出现(这是更大代码的一部分) 如何将文件名作为参数传递而不出错您必须在类中使用它: def read_contents(self,filename): with open(filename,'r') as f: lines=f.read().splitlines() print lines s=read_contents('input.txt') 或移除自我: class Tes

当尝试运行此程序时,错误作为两个必需参数抛出,self需要作为其中一个参数出现(这是更大代码的一部分)
如何将文件名作为参数传递而不出错

您必须在类中使用它:

def read_contents(self,filename):
    with open(filename,'r') as f:
        lines=f.read().splitlines()
        print lines

s=read_contents('input.txt')
或移除自我:

class Test:
    def read_contents(self, filename):
        with open(filename, 'r') as f:
            lines = f.read().splitlines()
            print lines

test = Test()
s = test.read_contents('input.txt')
def read_contents(filename):
    with open(filename, 'r') as f:
        lines = f.read().splitlines()
        print lines

s = read_contents('input.txt')