TypeError: __init__() missing 3 required positional arguments - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: TypeError: __init__() missing 3 required positional arguments (/thread-15517.html) |
TypeError: __init__() missing 3 required positional arguments - Pythonhelp82 - Jan-20-2019 Hi Guys, I am studying a mark scheme answer from a previous exam question and when following the mark scheme on Python , I am presented with an error: TypeError: __init__() missing 3 required positional arguments: 'MemberName', 'MemberID', and 'SubscriptionPaid' The focus is on practicing Methods: methods • SetMemberName • SetMemberID • SetSubscriptionPaid I have played around with the code and used various methods to pass the arguments, unfortunately, still presented with this error. Any help appreciated. class Member() : def __init__(self, MemberName, MemberID, SubscriptionPaid): self.__MemberName = "" self.__MemberID = "" self.__SubscriptionPaid = False def SetMemberName(self, Name): self.MemberName = Name def SetMemberID(self, ID): self.MemberID = ID def SetSubscriptionPaid(self, Paid): self.SubscriptioPaid = Paid class JuniorMember (Member): def __init__(self): super().__init__() self.DateOfBirth="" def SetDateOfBirth(self, Date): self.DateOfBirth = Date NewMember=JuniorMember() NewMember.SetMemberName("Ali") NewMember.SetMemberID("12345") NewMember.SetSubscriptionPaid(True) NewMember.SetDateOfBirth("12/11/2001") __init__ function of Member class has 3 arguments. When I am calling super().__init__() from JuniorMember class I am passing 3 arguments now - but it says NameError: name 'MemberName' is not defined super().__init__(MemberName, MemberID, SubscriptionPaid) RE: TypeError: __init__() missing 3 required positional arguments - stullis - Jan-20-2019 You're receiving the error because super().__init__() is calling the __init__() of the parent, which requires three arguments which are not present. With your correction, the interpreter is giving you a new error because the variables you're passing haven't been defined in the scope of __init__() neither as a variable inside the function nor as an argument passed in. To dummy out the arguments, do this: class Member() : def __init__(self, MemberName, MemberID, SubscriptionPaid): self.__MemberName = "" self.__MemberID = "" self.__SubscriptionPaid = False def SetMemberName(self, Name): self.MemberName = Name def SetMemberID(self, ID): self.MemberID = ID def SetSubscriptionPaid(self, Paid): self.SubscriptioPaid = Paid class JuniorMember (Member): def __init__(self): super().__init__("John Doe", "000000", False) self.DateOfBirth="" def SetDateOfBirth(self, Date): self.DateOfBirth = Date NewMember=JuniorMember() NewMember.SetMemberName("Ali") NewMember.SetMemberID("12345") NewMember.SetSubscriptionPaid(True) NewMember.SetDateOfBirth("12/11/2001") RE: TypeError: __init__() missing 3 required positional arguments - DeaD_EyE - Jan-20-2019 This should work. class JuniorMember(Member): def __init__(self, MemberName, MemberID, SubscriptionPaid): super().__init__(MemberName, MemberID, SubscriptionPaid) self.DateOfBirth = ""PS: I forgot to hit the post button :-/ RE: TypeError: __init__() missing 3 required positional arguments - snippsat - Jan-20-2019 It's Java written in Python,it's not your fault we see this often in education when have teacher that's come from a Java/C++ background. Just to show how it could be written in more pythonic way. Remove getters/setters and __ (is not private,it's name mangling that's not needed here), PEP-8 class Member() : def __init__(self, member_name, member_id, subscription_paid): self.member_name = member_name self.member_id = member_id self.subscription_paid = subscription_paid class JuniorMember (Member): def __init__(self,member_name, member_id, subscription_paid, date_of_birth): super().__init__(member_name, member_id, subscription_paid) self.date_of_birth = date_of_birthTest usage: >>> new_member = JuniorMember('Ali', 12345, True, "12/11/2001") >>> new_member.member_name 'Ali' >>> new_member.date_of_birth '12/11/2001'. >>> an_other_member = JuniorMember('Tom', 4444, True, "1/2/2018") >>> an_other_member.member_id 4444 >>> an_other_member.subscription_paid True >>> regular_member = Member('Kent', 9999, True) >>> regular_member.member_name 'Kent'But i don't want to give all data at once,use getters/setter? No it's simple attribute access,have to prove that need more that,if need more @property can be used.Use default arguments. def __init__(self,member_name='', member_id='', subscription_paid='', date_of_birth=''): >>> new_member = JuniorMember() >>> new_member.date_of_birth = '12/11/2001' >>> new_member.date_of_birth '12/11/2001' RE: TypeError: __init__() missing 3 required positional arguments - Pythonhelp82 - Jan-21-2019 (Jan-20-2019, 02:18 PM)DeaD_EyE Wrote: This should work. I remember trying that too and it resulted in the same error: TypeError: __init__() missing 3 required positional arguments: 'MemberName', 'MemberID', and 'SubscriptionPaid' (Jan-20-2019, 08:33 PM)snippsat Wrote: It's Java written in Python,it's not your fault we see this often in education when have teacher that's come from a Java/C++ background. This is a fantastic break down and very useful for a beginner and something I will be using definitely, however, I think the purpose of the task is to demonstrate set methods: • SetMemberName • SetMemberID • SetSubscriptionPaid TypeError: __init__() missing 3 required positional arguments: 'MemberName', 'MemberID', and 'SubscriptionPaid' RE: TypeError: __init__() missing 3 required positional arguments - snippsat - Jan-21-2019 (Jan-21-2019, 03:21 AM)Pythonhelp82 Wrote: TypeError: __init__() missing 3 required positional arguments: 'MemberName', 'MemberID', and 'SubscriptionPaid'As you have it now NewMember=JuniorMember('Ali', 4444, True) Set default arguments,the can use it like this. class JuniorMember(Member): def __init__(self, MemberName='', MemberID='', SubscriptionPaid=''): super().__init__(MemberName, MemberID, SubscriptionPaid) self.DateOfBirth = "" def SetDateOfBirth(self, Date): self.DateOfBirth = DateUse: >>> NewMember = JuniorMember() >>> NewMember.SetMemberName("Ali") >>> NewMember.SetMemberID("12345") >>> NewMember.MemberID '12345' >>> NewMember.MemberName 'Ali' RE: TypeError: __init__() missing 3 required positional arguments - Pythonhelp82 - Jan-24-2019 (Jan-21-2019, 03:37 PM)snippsat Wrote:(Jan-21-2019, 03:21 AM)Pythonhelp82 Wrote: TypeError: __init__() missing 3 required positional arguments: 'MemberName', 'MemberID', and 'SubscriptionPaid'As you have it now Thank you so much in helping me understand and fix this programming problem. |