I have come across the notion of class constructors in C++. But I have not yet found a way to initialize a class in Python using 2 or more different methods. Could anyone tell how to go about that?
Asked
Active
Viewed 1,661 times
0
-
1Look at `__init__` and `__new__` in the data model https://www.google.com/search?client=firefox-b-e&q=python+init+vs+new. Usually in Python you don't implement the constructor (`__new__`) but just use `__init__` to initalise a new instance of your class. – Dan Jun 02 '20 at 15:46
-
1Are you asking about constructors in general, or are you looking for an analog to C++'s overloaded constructors in Python? – ESilk Jun 02 '20 at 15:48
-
https://docs.python.org/3/tutorial/classes.html This is the standard way to go about constructors in general. Here is also a nice explanation https://www.youtube.com/watch?v=ZDa-Z5JzLYM. I hope this helps! :) – user78910 Jun 02 '20 at 16:00
-
Python does not have method overloading if it is what you are looking for. But you can have different class methods acting like a factory, that is, with different names, accepting their own argument list and returning object instances of the same or different type. – progmatico Jun 02 '20 at 16:04
-
@progmatico but that would not be like what __init__ provides, right? I don't want to create instances of car using methods car1 and car2. – levio_sa Apr 12 '21 at 17:40
-
No, but it is not so much different. For example, Instead of calling `c1 = Car(color="white")` you can call `Car.get_white_car()`. This does not create a new instance only to obtain another one from the method. Instead you create the instance as usual inside the method, which also runs init, and configure any additional or different settings there, then you return the instance. You can also process the init arguments or any additional arguments as you wish. – progmatico Apr 13 '21 at 11:12
-
The method also works when called over an existing Car instance, but that is not as clear as it should. – progmatico Apr 13 '21 at 11:15
-
Another approach is to capture the arguments for init in `*args` and `**kwargs` and process them accordingly, as mentioned in the answers. – progmatico Apr 13 '21 at 11:20
2 Answers
2
You don't need multiple constructors in python, you can use the following way to initialize if you have multiple such a case
class A:
def __init__(self, arg_1, arg_2=None):
self.arg_1 = arg_1
self.arg_2 = arg_2
So when you need to initialize an object of class A, you can use
a1 = A(2)
a2 = A(2, 4)
Though strictly speaking __init__
is not a constructor but an initialiser

Ratan Rithwik
- 36
- 3
-
If it is a question of having a few optional arguments, of course this a valid choice. See `help(int.from_bytes)` for an example of what I meant in the comment above, giving an alternate method to `int()` – progmatico Jun 02 '20 at 16:15
0
@Ratan Rithwik solution is correct but only only 2 cases
If you want to have as many case as you want, you can use **kwarg
one example with @thebjorn answer
EDIT: mixing 'standard' parameter (having default value) and kwargs
class Player:
def __init__(self, name='John', **kwargs):
self.name = name
self.last_name = kwargs.get('last_name')
p = Player(last_name='Doe')
print (p.name) # John
print (p.last_name) #Doe
p1 = Player('foo')
print (p1.name) # foo
print (p1.last_name) #None

DonKnacki
- 427
- 4
- 11
-
Can I provide default values there somehow? Assume None might be a potential acceptable value – levio_sa Apr 12 '21 at 18:01
-
yes, you can use 'standard' parameter with default value : see my edit – DonKnacki Apr 12 '21 at 22:00