我找不到一个明确的答案。据我所知,在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
你说呢?还有别的办法吗?
人们肯定更喜欢已经发布的解决方案,但由于还没有人提到这个解决方案,我认为为了完整性,值得一提。
@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
由于我最初的回答受到了批评,因为我的特殊用途构造函数没有调用(唯一的)默认构造函数,我在这里发布了一个修改版本,以尊重所有构造函数都应该调用默认构造函数的愿望:
class Cheese:
def __init__(self, *args, _initialiser="_default_init", **kwargs):
"""A multi-initialiser.
"""
getattr(self, _initialiser)(*args, **kwargs)
def _default_init(self, ...):
"""A user-friendly smart or general-purpose initialiser.
"""
...
def _init_parmesan(self, ...):
"""A special initialiser for Parmesan cheese.
"""
...
def _init_gouda(self, ...):
"""A special initialiser for Gouda cheese.
"""
...
@classmethod
def make_parmesan(cls, *args, **kwargs):
return cls(*args, **kwargs, _initialiser="_init_parmesan")
@classmethod
def make_gouda(cls, *args, **kwargs):
return cls(*args, **kwargs, _initialiser="_init_gouda")
如果你只需要__init__,使用num_holes=None作为默认值是可以的。
如果你想要多个独立的“构造函数”,你可以把它们作为类方法提供。这些方法通常称为工厂方法。在本例中,num_holes的默认值为0。
class Cheese(object):
def __init__(self, num_holes=0):
"defaults to a solid cheese"
self.number_of_holes = num_holes
@classmethod
def random(cls):
return cls(randint(0, 100))
@classmethod
def slightly_holey(cls):
return cls(randint(0, 33))
@classmethod
def very_holey(cls):
return cls(randint(66, 100))
现在创建一个这样的对象:
gouda = Cheese()
emmentaler = Cheese.random()
leerdammer = Cheese.slightly_holey()
class Cheese:
def __init__(self, *args, **kwargs):
"""A user-friendly initialiser for the general-purpose constructor.
"""
...
def _init_parmesan(self, *args, **kwargs):
"""A special initialiser for Parmesan cheese.
"""
...
def _init_gauda(self, *args, **kwargs):
"""A special initialiser for Gauda cheese.
"""
...
@classmethod
def make_parmesan(cls, *args, **kwargs):
new = cls.__new__(cls)
new._init_parmesan(*args, **kwargs)
return new
@classmethod
def make_gauda(cls, *args, **kwargs):
new = cls.__new__(cls)
new._init_gauda(*args, **kwargs)
return new