![]() |
Overload of constructor - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Overload of constructor (/thread-17569.html) |
Overload of constructor - psosmol - Apr-16-2019 Hello, I have for files AllOnesGA.py with the main(), GeneticAlgorithm.py,Population.py and Individual.py The cuestion is that pycharm mark as an error the overloading of the constructor thanks class Population: def __init__(self,populationSize): self.populationSize=individual_file[populationSize] def __init__(self,populationSize,chromosomeLength): self.population = individual_file.Individual[populationSize] for individualCount in range (0,populationSize): individual = individual_file(chromosomeLength) self.population[individualCount]= individual ...what I'm doing wrong ps:the error is
RE: Overload of constructor - buran - Apr-16-2019 I would say self.population = individual_file.Individual[populationSize]should be self.population = individual_file.Individual(populationSize)note brackets vs square brackets Also note you don't need __init__() twice. The second one simply override the first oneAnd based on all your questions so far I would suggest to take [once again] an introductory tutorial on OOP RE: Overload of constructor - psosmol - Apr-17-2019 Hello, I correct some mistake and now I have this new error genetic_algorithm.pyimport random import population_file import individual_file class GeneticAlgorithm: def __init__(self, populationSize, mutationRate, crossoverRate, elitismCount): self.populationSize=populationSize self.mutationRate=mutationRate self.crossoverRate=crossoverRate self.elitismCount=elitismCount def initPopulation(self,chromosomeLength): population = population_file.Population(self.populationSize, chromosomeLength) return population ........population_file.py import random class Population: def __init__(self,populationSize): self.populationSize=individual_file(populationSize) def __init__(self,populationSize,chromosomeLength): self.population = individual_file.Individual(populationSize) for individualCount in range (0,populationSize): individual = individual_file.Individual(chromosomeLength) self.population[individualCount]= individual def getIndividuals(self): return self.population ........individual_file.py import random class Individual: def __init__(self,chromosome): self.chromosome=chromosome def __init__(self,chromosomeLength): self.chromosome=[chromosomeLength] for gene in range (chromosomeLength): if 0.5 < random.randint(0,1): self.setGene(gene,1) else: self.setGene(gene,0) def getChromosome(self): return self.chromosome def getChromosomeLength(self): return self.chromosome.size def setGene(self,offset,gene): self.chromosome[offset]= gene def getGene(self,offset): return self.chromosome[offset] def setFitness(self,fitness): self.fitness(fitness) def getFitness(self): return self.fitness ........Thanks |