How can I declare a few methods with the same name, but with different numbers of parameters or different types in one class?
What must I change in the following class?>
This cannot work. No matter how many arguments you have, the name m
will be overriden with the second m
method.
class C:
def m(self):
print('m first')
def m(self, x):
print(f'm second {x}')
ci=C();
#ci.m() # will not work TypeError: m() missing 1 required positional argument: 'x'
ci.m(1) # works
The output will simple be:
m second 1