2011-08-21 5 views
4

가능한 중복 혼동 :
Understanding Python super()[파이썬] : 슈퍼()

클래스 B 서브 클래스 A, B의 __init__에서, 그래서 우리는이 같은의 __init__를 호출해야합니다 :

class B(A): 
    def __init__(self): 
     A.__init__(self) 

그러나

class B(A): 
    def __init__(self): 
     super(B, self).__init__() #or super().__init__() 

내 질문

은 다음과 같습니다 :

  1. super(B, self).__init__(self), 나는 이런 식으로 뭔가를 보았다? 반환 프록시 객체가 바운드 객체이기 때문에?

  2. super에서 두 번째 인수를 생략하고 반환 프록시 객체가 언 바운드 객체 ​​인 경우에는 super(B).__init__(self)을 써야하나요?

+1

기타 여러 가지 - 먼저 검색하십시오. [어떻게 - 비단뱀 - 슈퍼 - 할 - 옳은 일] (http://stackoverflow.com/questions/607186/how-does-pythons-super-do-the-right-thing) [어떻게 - 않습니다 -python-super-work-with-multiple-inheritance] (http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) [usage-of-python-3 - 슈퍼] (http://stackoverflow.com/questions/2771904/usage-of-python-3- 슈퍼) – agf

답변

5

super() 그래서 self가 암시 적으로 다른 메서드 호출처럼 __init__()에 전달되는 기본 클래스의 인스턴스를 반환합니다.

두 번째 질문에 관해서는 정확합니다. 두 번째 인수로 인스턴스가없는 super()을 호출하면 하위 클래스 인스턴스에서 생성 된 인스턴스가 아니라 클래스 자체에 대한 참조가 반환됩니다.

+0

나는 그것을 얻은 것 같아요. 고마워. – Alcott