How to set argument of an object?
so I want to know if there is a solution to make an argument which is not organised when I create an object.
class Joueur:
nbrJoueur=0
def __init__(self,c_pseudo,c_pv=25,c_sttat=0):
self.pseudo=c_pseudo
self.pv=c_pv
self.sttat=c_sttat
Joueur.nbrJoueur+=1
j1=Joueur("adel",250,0)
j2=Joueur("salah",c_sttat=10)
So like in j2
I had to make c_sttat=10
to avoid that it's the the c_pv
which take the value 10.
So I want to avoid that problem when I have a lot of arguments how could I do that?
Solution 1:
I think this may be what you want:
def __init__(self,c_pseudo, *, c_pv=25,c_sttat=0):
When you put *
in the parameter list, all the arguments after that position must be given with keywords. So if you try to write
j2 = Joueur("joseph", 10)
you'll get an error because you didn't name the second argument. This forces you to indicate whether it's c_pv
or c_sttat