在Python中从类调用函数时参数数目错误

2024-06-01 10:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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

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

^{pr2}$

它从IDLE返回以下错误:

TypeError: mutate() takes exactly 1 argument (2 given)

我怎么能解决这个问题?在


Tags: 代码inselfforlenifdefrange