Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个类使用多个构造函数的Python方式是什么?_Python_Oop_Constructor_Genetic Algorithm_Multiple Constructors - Fatal编程技术网

一个类使用多个构造函数的Python方式是什么?

一个类使用多个构造函数的Python方式是什么?,python,oop,constructor,genetic-algorithm,multiple-constructors,Python,Oop,Constructor,Genetic Algorithm,Multiple Constructors,我正在用python编写一些遗传算法。目前我正在看一本有java代码的书,所以最终我尝试编写我自己的自适应遗传算法来优化神经网络参数和层,我所有的神经网络代码都是使用keras的python…所以我只编写我自己的python代码,相当于书中的java代码 我目前设置它的方式存在的问题是,构造函数总是在第一条语句中将对象初始化为构造函数 从其他类似的问题中我看到,你可以使用classmethods来做这件事…你也可以调用do with…isinstanceOf method…你可以用init me

我正在用python编写一些遗传算法。目前我正在看一本有java代码的书,所以最终我尝试编写我自己的自适应遗传算法来优化神经网络参数和层,我所有的神经网络代码都是使用keras的python…所以我只编写我自己的python代码,相当于书中的java代码

我目前设置它的方式存在的问题是,构造函数总是在第一条语句中将对象初始化为构造函数

从其他类似的问题中我看到,你可以使用classmethods来做这件事…你也可以调用do with…isinstanceOf method…你可以用init method*args和**kwargs做一些事情…我想你可以用非类型参数做一些事情,这正是我试图做的…但我一直在忙着打开和关闭它,似乎无法在没有它的情况下让它工作添加我不喜欢的其他布尔参数

人们喜欢用什么方法来解决多重构造函数问题,你能用我尝试的方法来解决吗

我尝试使用多个构造函数的示例

class Individual(object):  

#Object Initialisation-->Generally called a constructor
#In python, a variable prefixed with _ is effectively private 
def __init__(self, chromosome, chromosomeLength, bool1):
    if(chromosomeLength is not None and bool1==False):
        self.chromosomeLength = chromosomeLength
        self.fitness = -100
        self.chromosome = np.empty(chromosomeLength)
        for gene in range(self.chromosomeLength):
                if(0.5 < np.random.randint(2)):
                    #self.setGene(gene,1)
                    #cls.chromosome[gene] = 1
                    self.chromosome[gene] = 1
                else:
                    #self.setGene(gene,0)
                    #cls.chromosome[gene] = 0
                    self.chromosome[gene] =0

    elif(chromosome is not None and bool1 ==True):

        self.chromosome = chromosome
        self.fitness = -1
        print ("All variable initialized")
谢谢


Matt

如果您希望在类中使用factory函数或方法从不同的变量集构造对象,则可以使用它们:

例如,用伪代码来说明这个想法

from_chromosomes(chromosomes):
    parse_chromosomes
    return Individual(*args, **kwargs)

from_ancestors(parents):
    parse_parents_genes
    return Individual(*args, **kwargs)

(...)

这可以与使用默认值结合使用。

以纯文本而不是图像的形式发布代码。有关代码格式设置帮助,请参阅。