Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

Passion, Grace & Fire.

super() 의 argument 본문

python

super() 의 argument

vincenthanna 2020. 11. 25. 19:26

super() 에 type, obj argument를 사용하는 경우:

 

class super(t: Any, obj: Any)

super(type) -> unbound super object super(type, obj) -> bound super object; requires isinstance(obj, type) super(type, type2) -> bound super object; requires issubclass(type2, type)

Typical use to call a cooperative superclass method: class C(B):

 

def meth(self, arg):
    super().meth(arg)

 

This works for class methods too: class C(B):

 

@classmethod
def cmeth(cls, arg):
    super().cmeth(arg)

 

 

super(<type>, <self>) 의 경우 <type>의 부모 타입으로 돌려준다.

 

class A:
    def __init__(self):
        print("a", end='')

class B(A):
    def __init__(self):
        super(B, self).__init__()
        print("b", end='')

class C(B):
    def __init__(self):
        super(C, self).__init__()
        print("c", end='')

class D(C):
    def __init__(self):
        super(D, self).__init__()
        print("d", end='')

d = D()

 

위 코드에서 class D의 super()호출부에서 type에 따른 출력:

 

super(D, self).__init__() 인 경우 : D의 부모인 C의 생성자가 호출

    abcd

 

super(C, self).__init__() 인 경우 : C의 부모인 B의 생성자가 호출

    abc

 

super(B, self).__init__() 인 경우 : B의 부모인 A의 생성자가 호출

    ad

 

super(A, self).__init__() 인 경우 : A가 처음이므로 아무것도 하지 않는다.

    d

 

 

 

Comments