我找不到一个明确的答案。据我所知,在Python类中不能有多个__init__函数。那么如何解决这个问题呢?

假设我有一个名为Cheese的类,它具有number_of_holes属性。我怎么能有两种方法来创建奶酪对象…

其中一个需要像这样的洞的数量:帕玛森=奶酪(num_holes = 15)。 还有一个不带参数,只是随机number_of_holes属性:gouda = Cheese()。

我只能想到一种方法来做到这一点,但这似乎很笨拙:

class Cheese():
    def __init__(self, num_holes = 0):
        if (num_holes == 0):
            # Randomize number_of_holes
        else:
            number_of_holes = num_holes

你说呢?还有别的办法吗?


当前回答

最好的答案是上面关于默认参数的那个,但我很高兴写这个,而且它确实符合“多个构造函数”的要求。使用风险自负。

新方法怎么样?

典型的实现通过使用super(currentclass, cls)调用超类的new()方法来创建类的新实例。使用适当的参数New (cls[,…]),然后在返回它之前根据需要修改新创建的实例。”

因此,您可以让新方法通过附加适当的构造函数方法来修改类定义。

class Cheese(object):
    def __new__(cls, *args, **kwargs):

        obj = super(Cheese, cls).__new__(cls)
        num_holes = kwargs.get('num_holes', random_holes())

        if num_holes == 0:
            cls.__init__ = cls.foomethod
        else:
            cls.__init__ = cls.barmethod

        return obj

    def foomethod(self, *args, **kwargs):
        print "foomethod called as __init__ for Cheese"

    def barmethod(self, *args, **kwargs):
        print "barmethod called as __init__ for Cheese"

if __name__ == "__main__":
    parm = Cheese(num_holes=5)

其他回答

人们肯定更喜欢已经发布的解决方案,但由于还没有人提到这个解决方案,我认为为了完整性,值得一提。

@classmethod方法可以修改为提供一个不调用默认构造函数(__init__)的替代构造函数。相反,使用__new__创建实例。

如果不能根据构造函数参数的类型选择初始化类型,并且构造函数不共享代码,则可以使用此方法。

例子:

class MyClass(set):

    def __init__(self, filename):
        self._value = load_from_file(filename)

    @classmethod
    def from_somewhere(cls, somename):
        obj = cls.__new__(cls)  # Does not call __init__
        super(MyClass, obj).__init__()  # Don't forget to call any polymorphic base class initializers
        obj._value = load_from_somewhere(somename)
        return obj

这些对于你的实现来说都是很好的想法,但是如果你要向用户展示一个奶酪制作界面。他们不关心奶酪有多少洞,也不关心奶酪的内部成分。你代码的用户只想要“豪达干酪”或“帕尔马干酪”,对吧?

所以为什么不这样做呢:

# cheese_user.py
from cheeses import make_gouda, make_parmesean

gouda = make_gouda()
paremesean = make_parmesean()

然后你可以使用上面的任何方法来实际实现这些函数:

# cheeses.py
class Cheese(object):
    def __init__(self, *args, **kwargs):
        #args -- tuple of anonymous arguments
        #kwargs -- dictionary of named arguments
        self.num_holes = kwargs.get('num_holes',random_holes())

def make_gouda():
    return Cheese()

def make_paremesean():
    return Cheese(num_holes=15)

这是一种很好的封装技术,而且我认为它更具有python性。对我来说,这种做事的方式更符合鸭子的打字方式。你只是在请求一个gouda对象,而不关心它是什么类。

如果你想使用可选参数,所有这些答案都很好,但python的另一种可能性是使用类方法生成工厂风格的伪构造函数:

def __init__(self, num_holes):

  # do stuff with the number

@classmethod
def fromRandom(cls):

  return cls( # some-random-number )

最好的答案是上面关于默认参数的那个,但我很高兴写这个,而且它确实符合“多个构造函数”的要求。使用风险自负。

新方法怎么样?

典型的实现通过使用super(currentclass, cls)调用超类的new()方法来创建类的新实例。使用适当的参数New (cls[,…]),然后在返回它之前根据需要修改新创建的实例。”

因此,您可以让新方法通过附加适当的构造函数方法来修改类定义。

class Cheese(object):
    def __new__(cls, *args, **kwargs):

        obj = super(Cheese, cls).__new__(cls)
        num_holes = kwargs.get('num_holes', random_holes())

        if num_holes == 0:
            cls.__init__ = cls.foomethod
        else:
            cls.__init__ = cls.barmethod

        return obj

    def foomethod(self, *args, **kwargs):
        print "foomethod called as __init__ for Cheese"

    def barmethod(self, *args, **kwargs):
        print "barmethod called as __init__ for Cheese"

if __name__ == "__main__":
    parm = Cheese(num_holes=5)

使用num_holes=None作为默认值。然后检查num_holes是否为None,如果是,则随机化。这就是我通常看到的情况。

更完全不同的构造方法可能需要一个返回cls实例的类方法。