'Self treated like a normal argument [duplicate]

So, I have the following class:

class GeneticAlgorithm():
    def breed_population(self, agents, fitness=0.10, crossover_rate=0.50, mutation_rate=0.10, mutation_degree=0.10, mutate=True):
        pass


    def crossover(self, child, parent_one, parent_two, crossover_rate, mutation_rate, mutation_degree, mutate):
        pass

When I try to run it, I get this error message:

agents = GeneticAlgorithm.breed_population(agents)
TypeError: breed_population() missing 1 required positional argument: 'agents'

Meaning that, for some reason, self is being treated like an actual argument instead of just giving the class access to all functions within the class.

How do I fix this?



Solution 1:[1]

The problem you are having is that you trying to call the method on the class without creating an object.

agents = GeneticAlgorithm.breed_population(agents)

Should be:

ga_obj = GeneticAlgorithm()
agents = ga_obj.breed_population(agents)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cargo23