Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

python - __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 :
")
print ("your name is :" +a)
b=raw_input("Enter your USN :
")
print ("Your USN is:" ,int(b))
c=raw_input("Enter your branch :
")
print ("your entered baranch is" +c)
d=raw_input("Enter your current semester :
")
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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...