python 2.7 __init __()需要2个参数(给定3个) [英] python 2.7 __init__() takes exactly 2 arguments (3 given)
问题描述
我有这些课程。
人是父类,学生是子类:
I've got These classes. person is the parent class and student is the child class:
#Person class
class person(object):
def __init__(self, name):
self.name = name
def pr(self):
print "I'm " + self.name
#Student class
from personClass import person
class student(person):
def __init__(self, avr, name):
self.avr = avr
super(student, self).__init__(self, name)
def pr(self):
super(student, self).pr(self)
print "My avrage is " + str(self.avr) + "\n"
我在网上收到此错误17当我尝试创建我的类的实例时 init ()只需要2个参数(3个给定)和
个人。 init (self,name)有效精细。
我的代码有什么问题,我如何在python 2.7中使用super?
I get this error on line 17 when I try to make an instance of my class init() takes exactly 2 arguments (3 given) and person.init(self, name) works fine. What is wrong with my code and how do I use super in python 2.7?
推荐答案
如果你使用超级,您不会将 self
传递给目标方法。隐式传递。
If you are using super, you don't pass self
to the target method. It is passed implicitly.
super(student, self).__init__(name)
总共有2个参数(self,name)。当您通过 self
时,这总共为3(自我,自我,姓名)。
That's 2 arguments total (self, name). When you passed self
, that was 3 total (self, self, name).
这篇关于python 2.7 __init __()需要2个参数(给定3个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!