6 Replies - 294 Views - Last Post: 21 January 2019 - 04:20 AM Rate Topic: -----

#1 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 19

TypeError: __init__() missing 3 required positional arguments

Posted 20 January 2019 - 05:04 AM

OO Newbie here!

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")




Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: __init__() missing 3 required positional arguments

#2 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: TypeError: __init__() missing 3 required positional arguments

Posted 20 January 2019 - 05:07 AM

Explain what you think the error is saying.
Was This Post Helpful? 0
  • +
  • -

#3 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 19

Re: TypeError: __init__() missing 3 required positional arguments

Posted 20 January 2019 - 05:13 AM

View Postandrewsw, on 20 January 2019 - 05:07 AM, said:

Explain what you think the error is saying.


__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

[code]super().__init__(MemberName, MemberID, SubscriptionPaid)[code/]
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: TypeError: __init__() missing 3 required positional arguments

Posted 20 January 2019 - 06:25 AM

Post your updated code.

You do not have a member called MemberName.
Was This Post Helpful? 0
  • +
  • -

#5 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-January 19

Re: TypeError: __init__() missing 3 required positional arguments

Posted 20 January 2019 - 08:42 PM

My updated code:

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):
[b]    def __init__(self, MemberName, MemberID, SubscriptionPaid):
        super().__init__(MemberName, MemberID, SubscriptionPaid)[/b]
        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")



Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6736
  • View blog
  • Posts: 27,734
  • Joined: 12-December 12

Re: TypeError: __init__() missing 3 required positional arguments

Posted 21 January 2019 - 01:42 AM

NewMember=JuniorMember() Your JuniorMember class requires 3 arguments in construction.

If you want to initiate a JuniorMember without first providing a MemberName and MemberID, etc., then you need to define an __init__ method that either takes no arguments, or allows optional arguments. You would do the same for the parent class, as necessary.

That is, Python does not have the concept of multiple constructors, so either provide all (3) required arguments, or perhaps construct an __init__ method with optional arguments, def __init__(self, x = 42):. This means that you can construct with either

foo = JuniorMember() # x defaults to 42
bar = JuniorMember(50) # x has the value 50

Was This Post Helpful? 0
  • +
  • -

#7 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 507
  • View blog
  • Posts: 1,597
  • Joined: 27-December 13

Re: TypeError: __init__() missing 3 required positional arguments

Posted 21 January 2019 - 04:20 AM

Your Member class is a bit strange (In my opinion).

In the __init__() function you include name, ID and subscription as parameters.
So, already when the instances are created you need to provide those values.
You never use those values, however !!!

Then you have three Set-functions; these are designed to assign the same values as the __init__().
I guess you should decide to use one stringent structure.
Maybe call the three Set-functions from the __init__() - that would be a nice structure.

The use of self.__something is ok, but you never attempt using those attributes.
What is your intention here?

All of this trickles down to the child class.
Fix the parent class and things will (hopefully) be clearer to yourself, helping in making the rest of the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1