How to implement constructor overloading correctly in python?
Python does NOT support any constructor or function overloading. However, it doesn't need to. Instead, it supports setting default parameters.
class Tes:
def __init__(self, a, b="default_value_b"):
self.va = a
self.vb = b
print(self.va)
print(self.vb)
The question changes a little if you are using typing. Typing is a relatively new to Python and purely optional. Typing allows you to statically validate your code against expectations, and it can ease writing code by providing live assistance.
In your simple case, you would not use overloading, because it only defines an optional parameter. The type of the parameter stays the same regardless. With Mypy type annotations, your code can be written as follows:
class Tes:
def __init__(self, a: str, b: str = "default value for b") -> None:
self.va = a
self.vb = b
print(self.va)
print(self.vb)
If you want to allow None as a value for b, change the signature to:
def __init__(self, a: str, b: Optional[str] = "default value for b") -> None:
Optional can be imported from typing:
from typing import Optional