+ 1

How to do constructor overloading in python?

how to achieve constructor overloading in python? i tried making two __init__ function but did not worj

19th May 2019, 10:38 AM
Apeal Tiwari
Apeal Tiwari - avatar
2 Answers
+ 4
If you define several __init__ methods, each new method will just override the preceding definition(s), so there'll always be only one __init__ method. There's no need for constructor overloading since you can just use variable arguments and make your __init__ method flexible enough to react to all kinds of object initializations: def __init__(self, *args): print(len(args), 'were passed') if args: print('first argument is of type', type(args[0])) etc.
19th May 2019, 1:01 PM
Anna
Anna - avatar
0
def __init__(self, *args, **kwargs): ...
19th May 2019, 12:54 PM
Steven M
Steven M - avatar