0

TypeError: __init__() takes 3 positional arguments but 7 were given ???

HELP!! Why is this code giving me "TypeError: __init__() takes 3 positional arguments but 7 were given" class Homos: def __init__(self, etnia, religião, altura, literacia): self.etnia = etnia self.religião = religião self.altura = altura self.literacia = literacia #definir uma subclasse Homos de nome Femme: class Femme(Homos): def __init__(self, nome, salario): self.nome = nome self.salario = salario #definir uma subclasse Homos de nome Home: class Home(Homos): def __init__(self, nome, salario): sel.nome = nome self.salario = salario def __eq__(self, other): if Home.salario == Femme.salario: print("EQUALITE") else: print("DESIGUALDADE") H1 = Home("Carlos", "afro", "catolico", 1.80, "Uni", 40000) F1 = Femme("Ana", "caucasiana", "budista", 1.77, "Uni", 30000) Thank you!!

13th Mar 2019, 5:58 PM
Abel Pinheiro
Abel Pinheiro - avatar
14 odpowiedzi
+ 6
It's bcz constructor( __init__) method of class Home takes just 3 arguments viz. self, nome and salario but you're passing 7 arguments while creating its object. You just need to pass for arguments nome and salario so just 2 explicitly. And same is the case with Femme class as well. And by the next time, kindly consider sharing the link of the code snippets from the code playground rather than posting the entire code over here.
13th Mar 2019, 6:05 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 6
That's not how it works. You can refer to this website for clearing you concept about super() https://realpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK-super/
14th Mar 2019, 1:27 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 6
It's alright. Don't worry 😁 It's not actually like doubling the code. Like you've 4 attributes in Homos class which will be used in both Femme and Home classes in addition with two of their own attributes viz. nome and salario. But since you're inheriting Homos class, you're not defining those 4 attributes again in the inherited classes. That's where it's helpful. It's actually reducing the codes.
20th Mar 2019, 12:58 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 5
Nope. Even it's inheriting, it doesn't work that way. If you want to call the constructor of parent class from the child class, you must use super method in the child class. You can use super in the child class as: super(Home, self).__init__(arguments) Try reading about super method in detail.
13th Mar 2019, 6:21 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 5
Here's the corrected one: class Homos: def __init__(self, etnia, religião, altura, literacia): self.etnia = etnia self.religião = religião self.altura = altura self.literacia = literacia #definir uma subclasse Homos de nome Femme: class Femme(Homos): def __init__(self, nome, salario,etnia, religião, altura, literacia): super(Femme, self).__init__(etnia, religião, altura, literacia) self.nome = nome self.salario = salario #definir uma subclasse Homos de nome Home: class Home(Homos): def __init__(self, nome, salario, etnia, religião, altura, literacia): super(Home, self).__init__(etnia, religião, altura, literacia) self.nome = nome self.salario = salario def __eq__(self, other): if Home.salario == Femme.salario: print("EQUALITE") else: print("DESIGUALDADE") H1 = Home("Carlos", "afro", "catolico", 1.80, "Uni", 40000) F1 = Femme("Ana", "caucasiana", "budista"
20th Mar 2019, 12:15 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 4
I pinpointed you to learn and see you did it in a little better way. Just few errors. __init__() method of class Home or Femme takes only three parameters viz. self, nome, salario but when you're creating their instances i.e. H1 and F1 you're passing 6 arguments and so you're getting error that __init__ method takes 3 arguments but 7 given. Moreover, in the super method in the Home and Femme class, you're passing 4 arguments which is not defined within the class or the method.
20th Mar 2019, 12:15 PM
Шащи Ранжан
Шащи Ранжан - avatar
+ 1
But since it`s a subclass from the class Homos, that has the other arguments, shouldn't it work? Thanks for your help! :)
13th Mar 2019, 6:08 PM
Abel Pinheiro
Abel Pinheiro - avatar
+ 1
But this line here isn`t just doubling code: def __init__(self, nome, salario, etnia, religião, altura, literacia): super(Home, self).__init__(etnia, religião, altura, literacia) I thought that by using the super() method I could access the attributes of the Superclass without needing to retype them again! Ain`t this true? Because if I do use the same attributes all over again in the subclass Femme or Home as used in the class Homos (etnia, religião, altura, literacia) what`s the sense of using the super() method? And thank you for your much appreciated help. And I am sorry if I seemed to be rude with the pinpoint commentary but it was just my frustation speaking. Sorry for that! :)
20th Mar 2019, 12:36 PM
Abel Pinheiro
Abel Pinheiro - avatar
+ 1
Ok, I understood now!!! :D And you know what, I`ve also corrected my code, thanks to you and a friend of mine now is working ;). I also had and error on the __eq__ method. It was pointing somewhere else (etnia) rather than the salario. Now everything is ok. Thanks to you and to him. MUITO MUITO OBRIGADO, Shashi Ranjan :)
20th Mar 2019, 2:54 PM
Abel Pinheiro
Abel Pinheiro - avatar
20th Mar 2019, 2:56 PM
Abel Pinheiro
Abel Pinheiro - avatar
0
Or should I, when creating the objcts, instead of referring to the subclass Home, instead refer to the class Homos?
13th Mar 2019, 6:10 PM
Abel Pinheiro
Abel Pinheiro - avatar
0
RRrgghhhhh!! Now it says invalid syntax; class Homos: def __init__(self, etnia, religião, altura, literacia): self.etnia = etnia self.religião = religião self.altura = altura self.literacia = literacia #definir uma subclasse Homos de nome Femme: class Femme(Homos): def super(Homos, self).__init__(self, nome, salario): self.nome = nome self.salario = salario #definir uma subclasse Homos de nome Home: class Home(Homos): def super(Homos, self).__init__(self, nome, salario): sel.nome = nome self.salario = salario def __eq__(self, other): if Home.salario == Femme.salario: print("EQUALITE") else: print("DESIGUALDADE") H1 = Home("Carlos", "afro", "catolico", 1.80, "Uni", 40000) F1 = Femme("Ana", "caucasiana", "budista", 1.77, "Uni", 30000) H1==F1 The super method explanation from Sololearn doesn`t help much!!! : /
13th Mar 2019, 6:32 PM
Abel Pinheiro
Abel Pinheiro - avatar
0
I am sorry, but I`ve tried a lot of possibilites but still with the same error!! class Homos: def __init__(self, etnia, religião, altura, literacia): self.etnia = etnia self.religião = religião self.altura = altura self.literacia = literacia #definir uma subclasse Homos de nome Femme: class Femme(Homos): def __init__(self, nome, salario): super(Femme, self).__init__(etnia, religião, altura, literacia) self.nome = nome self.salario = salario #definir uma subclasse Homos de nome Home: class Home(Homos): def __init__(self, nome, salario): super(Home, self).__init__(etnia, religião, altura, literacia) sel.nome = nome self.salario = salario def __eq__(self, other): if Home.salario == Femme.salario: print("EQUALITE") else: print("DESIGUALDADE") H1 = Home("Carlos", "afro", "catolico", 1.80, "Uni", 40000) F1 = Femme("Ana", "caucasiana", "budista", 1.77, "Uni", 30000) H1==F1 ALSO class Homos: def __init__(self, etnia, religião, altura, literacia): self.etnia = etnia self.religião = religião self.altura = altura self.literacia = literacia #definir uma subclasse Homos de nome Femme: class Femme(Homos): def __init__(self, nome, salario): super().__init__(etnia, religião, altura, literacia) self.nome = nome self.salario = salario #definir uma subclasse Homos de nome Home: class Home(Homos): def __init__(self, nome, salario): super().__init__(etnia, religião, altura, literacia) sel.nome = nome self.salario = salario def __eq__(self, other): if Home.salario == Femme.salario: print("EQUALITE") else: print("DESIGUALDADE") H1 = Home("Carlos", "afro", "catolico", 1.80, "Uni", 40000) F1 = Femme("Ana", "caucasiana", "budista", 1.77, "Uni", 30000) H1==F1
20th Mar 2019, 10:12 AM
Abel Pinheiro
Abel Pinheiro - avatar
0
If someone could really help me and not pinpoint me to other place to see this or that, because I`m not really accomplishing this. That would be very much appreciated! Thank you very much.
20th Mar 2019, 10:14 AM
Abel Pinheiro
Abel Pinheiro - avatar