Sahana Patel
Sahana Patel

Reputation: 21

__init__() takes exactly 2 arguments (1 given)?

I would like to know where am lagging, Looking for your advices..

class Student_Record(object):

    def __init__(self,s):
        self.s="class_Library"
        print"Welcome!! take the benifit of the library"

    def Student_details(self):
        print " Please enter your details below"

a=raw_input("Enter your name :\n")
print ("your name is :" +a)
b=raw_input("Enter your USN :\n")
print ("Your USN is:" ,int(b))
c=raw_input("Enter your branch :\n")
print ("your entered baranch is" +c)
d=raw_input("Enter your current semester :\n")
print ("your in the semester",int(d))
rec=Student_Record()
rec.Student_details(self)

I am getting this error ..

TypeError: init() takes exactly 2 arguments (1 given)

Upvotes: 0

Views: 6478

Answers (3)

Nishant Bhakta
Nishant Bhakta

Reputation: 2987

Your code should be like this..(python indent):

class Student_Record(object):

    def __init__(self,s="class_Library"):
        self.s=s
        print"Welcome!! take the benifit of the library"

    def Student_details(self):
        print " Please enter your details below"
        a=raw_input("Enter your name :\n")
        print ("your name is :" +a)
        b=raw_input("Enter your USN :\n")
        print ("Your USN is:" ,int(b))
        c=raw_input("Enter your branch :\n")
        print ("your entered baranch is" +c)
        d=raw_input("Enter your current semester :\n")
        print ("your in the semester",int(d))

rec=Student_Record()

rec.Student_details()

s in def __init__ should have a default value or you can pass a value from rec=Student_Record().

Upvotes: 0

knh170
knh170

Reputation: 2990

if you do

class Student_Record(object):

    def __init__(self, s):
        self.s = ""

    def Student_details(self):
        print " Please enter your details below"

when you create the object of class Student_Record it should accept a parameter despite for itself (self). so it looks like:

record = Student_Record("text")

and in __init__ you can do whatever with the passed-in variable s. For example, self.s = s and you can call it anywhere in the class with self.s because it has been initialized.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121186

Your Student_Record.__init__() method takes two arguments, self and s. self is provided for you by Python, but you failed to provide s.

You are ignoring s altogether, drop it from the function signature:

class Student_Record(object):
    def __init__(self):
        self.s = "class_Library"
        print"Welcome!! take the benifit of the library"

Next, you are calling the method rec.Student_details() passing in an argument, but that method only takes self, which is already provided for you by Python. You don't need to pass it in manually, and in your case the name is not even defined in that scope.

Upvotes: 4

Related Questions