2016-07-29 4 views
0
class car(object): 

    def __init__(self, make, model, year): 
     self.make = make 
     self.model = model 
     self.year = year 
     self.odometer_reading = 0 

class electricCar(car): 
    def __init__(self, make, model, year): 
     super().__init__(make, model, year) 

tesla = electricCar('tesla', 'model s', 2016) 
print tesla.get_descriptive_name() 

TypeError: super() takes at least 1 argument (0 given)python super() 함수에 오류가 있습니까?

super() 함수의 문제점은 무엇입니까? (인수)없이

+0

의 인스턴스()'당신의 슈퍼 클래스, 난의 이름으로'슈퍼를 교체합니다. 이자형. '자동차 '. 아니면'super'를 원한다면,'super (electricCar, self) .__ init __ (make, model, year)' – BusyAnt

+0

어떤 파이썬 버전을 사용하고 있습니까? [super]() 구문은 [공식 문서] (https://docs.python.org/2/library/functions.html#super)에서 볼 수 있습니다. Python 2의 경우 유형 인수로 하위 클래스 이름을 지정해야합니다. Python3의 경우 – RedBaron

+0

방금 ​​언어를 배우면 6 세 * 인 Python 버전으로 시작하는 이유는 무엇입니까? 최신 버전부터 시작하십시오. Python2는 이전 시스템/라이브러리와의 하위 호환성을 위해 필요한 사용자 만 사용해야합니다. – Bakuriu

답변

7

super()python3 도입되었다 여기 python2 구현이다. python2python3에 대한

일반 상속 문법 문제에 대한
class electricCar(car): 
    def __init__(self, make, model, year): 
     super(electricCar,self).__init__(make, model, year) 

당신이 참조 할 수 this question

3

당신은 파이썬 3 구문을 사용하려고하는 것 같습니다,하지만 당신은 그에서 파이썬 2를 사용하는 버전 당신은 super 함수에 인수로 현재 클래스와 인스턴스를 전달해야합니다 당신은 USI는

super(electricCar, self).__init__(make, model, year) 
+0

도움을 주신 모든 분들께 감사드립니다. – npkp

0

경우 ng 파이썬 2 당신은 명시 적으로 인스턴스를 super 메서드로 전달해야합니다. 파이썬 3 이상에서는 인스턴스 변수가 암시 적으로 전달되므로이를 지정할 필요가 없습니다. 여기 self 클래스 car

super(car, self).__init__(make, model, year) 
관련 문제