I can't find a definitive answer for this. AFAIK, you can't have multiple __init__
functions in a Python class. So what is a good way to 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 a better way?