【转】python_类中两个构造函数

类中两个构造函数

@classmethod
    def today(cls):
        t = time.localtime()
        return cls(t.tm_year, t.tm_mon, t.tm_mday)

 验证后发现这个方法还是不方便

外部并不会认为它是一个构造函数,在这个函数里调用类的私有函数,会报错要求先实例化

可以利用可变参数的方法实现两种构造:

    def __init__(self,fn,h,h2=-1.000):
        self.height = h
        self.fuzzy_number = fn
        self.height2 = h2
        if self.height2 == -1.000:
            self.cacuPerimeters2()
            self.cacuArea2()
        else:
            self.cacuPerimeters()
            self.cacuArea()
原文地址:https://www.cnblogs.com/AHappyBird/p/10043111.html