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

后端 未结 13 2125
醉梦人生
醉梦人生 2020-11-22 07:12

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

13条回答
  •  攒了一身酷
    2020-11-22 07:40

    Use num_holes=None as a default, instead. Then check for whether num_holes is None, and if so, randomize. That's what I generally see, anyway.

    More radically different construction methods may warrant a classmethod that returns an instance of cls.

提交回复
热议问题