在Python中使用多个构造函数的干净的Python方式是什么?

2024-05-03 15:32:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

  1. 一个像这样的洞:parmesan = Cheese(num_holes = 15)
  2. 一个不带参数,只随机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

你怎么说?还有别的办法吗


Tags: of对象方法函数答案number参数属性
3条回答

我们当然应该更喜欢已经发布的解决方案,但因为还没有人提到这个解决方案,我认为完整性值得一提

可以修改@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

实际上None对于“神奇”值来说要好得多:

class Cheese():
    def __init__(self, num_holes = None):
        if num_holes is None:
            ...

现在,如果您想完全自由地添加更多参数:

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

为了更好地解释*args**kwargs的概念(您实际上可以更改这些名称):

def f(*args, **kwargs):
   print 'args: ', args, ' kwargs: ', kwargs

>>> f('a')
args:  ('a',)  kwargs:  {}
>>> f(ar='a')
args:  ()  kwargs:  {'ar': 'a'}
>>> f(1,2,param=3)
args:  (1, 2)  kwargs:  {'param': 3}

http://docs.python.org/reference/expressions.html#calls

如果只需要__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()

相关问题 更多 >