什么是实现多个构造函数的干净的“pythonic”方式?

新手上路,请多包涵

我找不到明确的答案。据我所知,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

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

原文由 winsmith 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 213
2 个回答

实际上 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

原文由 vartec 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

原文由 Ber 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题