In Python, overloading of functions (and the constructor, in fact, is a function) is not in principle. In your case, the second constructor is always called, because the def
instruction means "create a function object and assign this object to the written name", that is, first you assign the name __init__
one function (with the parameter b
), and then the same name - another function , already without parameter b
.
The behavior you are seeking can be obtained using the default parameters, for example:
def __init__(self, b=None): # Здесь лучше придумать что-то отличное от проверки на None # больше соответствующее вашей задаче. Возможно, вам просто подойдет # какое-то значение по умолчанию. if b is None: pass else: pass # ... a = a() # b будет равно None по умолчанию b = a(42) # b будет равно 42