从Python中的类调用函数时,参数数量错误

Stu*_*ans 0 python syntax-error

我正在尝试在python中编写遗传算法的实现。它说在那里,当只允许一个时,我用两个参数来调用它,但是我确定我不允许。

以下是相关代码:

class GA:
    def __init__(self, best, pops=100, mchance=.07, ps=-1):
        import random as r

        self.pop = [[] for _ in range(pops)]

        if ps == -1:
            ps = len(best)

        for x in range(len(self.pop)): #Creates array of random characters
            for a in range(ps):
                self.pop[x].append(str(unichr(r.randint(65,122))))

    def mutate(array):
        if r.random() <=  mchance:
            if r.randint(0,1) == 0:
                self.pop[r.randint(0, pops)][r.randint(0, ps)] +=1
            else:
                self.pop[r.randint(0, pops)][r.randint(0, ps)] -=1
Run Code Online (Sandbox Code Playgroud)

这是我初始化并从类中调用时的代码:

a = GA("Hello",10,5)
a.mutate(a.pop)
Run Code Online (Sandbox Code Playgroud)

从IDLE返回以下错误:

TypeError: mutate() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Ble*_*der 5

类的方法会自动传递该类的实例作为它们的第一个参数(self按惯例命名):

def mutate(self, array):
Run Code Online (Sandbox Code Playgroud)