What is a clean, pythonic way to have multiple constructors in Python?

I can't find a definitive answer for this. AFAIK, you can't have multiple __init__ functions in a Python class. So how do I solve this problem?

Suppose I have an class called Cheese with the number_of_holes property. How can I have two ways of creating cheese-objects...

  • one that takes a number of holes like this: parmesan = Cheese(num_holes = 15)
  • and one that takes no arguments and just randomizes the number_of_holes property: gouda = Cheese()
  • I can think of only one way to do this, but that seems kinda clunky:

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

    What do you say? Is there another way?


    Actually None is much better for "magic" values:

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

    Now if you want complete freedom of adding more parameters:

    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())
    

    To better explain the concept of *args and **kwargs (you can actually change these names):

    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


    Using num_holes=None as the default is fine if you are going to have just __init__ .

    If you want multiple, independent "constructors", you can provide these as class methods. These are usually called factory methods. In this case you could have the default for num_holes be 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))
    

    Now create object like this:

    gouda = Cheese()
    emmentaler = Cheese.random()
    leerdammer = Cheese.slightly_holey()
    

    如果您想使用可选参数,所有这些答案都非常出色,但另一种Pythonic可能性是使用classmethod来生成工厂样式的伪构造函数:

    def __init__(self, num_holes):
    
      # do stuff with the number
    
    @classmethod
    def fromRandom(cls):
    
      return cls( # some-random-number )
    
    链接地址: http://www.djcxy.com/p/26284.html

    上一篇: 我如何获得本地安装的Python模块列表?

    下一篇: 在Python中有多个构造函数的干净,pythonic方法是什么?