development

파이썬 슈퍼 메서드 및 호출 대안

big-blog 2021. 1. 6. 20:44
반응형

파이썬 슈퍼 메서드 및 호출 대안


수퍼 클래스 메서드를 호출해야하는 모든 예를 볼 수 있습니다.

super(SuperClass, instance).method(args)

다음을 수행하는 데 단점이 있습니까?

SuperClass.method(instance, args)

다음 상황을 고려하십시오.

class A(object):
    def __init__(self):
        print('Running A.__init__')
        super(A,self).__init__()
class B(A):
    def __init__(self):
        print('Running B.__init__')        
        # super(B,self).__init__()
        A.__init__(self) 

class C(A):
    def __init__(self):
        print('Running C.__init__')
        super(C,self).__init__()
class D(B,C):
    def __init__(self):
        print('Running D.__init__')
        super(D,self).__init__()

foo=D()

따라서 클래스는 소위 상속 다이아몬드를 형성합니다.

    A
   / \
  B   C
   \ /
    D

코드를 실행하면

Running D.__init__
Running B.__init__
Running A.__init__

그 때문에 나쁜 C__init__스킵. 때문에 그 이유는 B__init__호출 A__init__직접.

의 목적은 super상속 다이아몬드를 해결 하는 것입니다 . 주석을 제거하면

# super(B,self).__init__()

및 주석 처리

A.__init__(self) 

코드는 더 바람직한 결과를 산출합니다.

Running D.__init__
Running B.__init__
Running C.__init__
Running A.__init__

Now all the __init__ methods get called. Notice that at the time you define B.__init__ you might think that super(B,self).__init__() is the same as calling A.__init__(self), but you'd be wrong. In the above situation, super(B,self).__init__() actually calls C.__init__(self).

Holy smokes, B knows nothing about C, and yet super(B,self) knows to call C's __init__? The reason is because self.__class__.mro() contains C. In other words, self (or in the above, foo) knows about C.

So be careful -- the two are not fungible. They can yield vastly different results.

Using super has pitfalls. It takes a considerable level of coordination between all the classes in the inheritance diagram. (They must, for example, either have the same call signature for __init__, since any particular __init__ would not know which other __init__ super might call next, or else use **kwargs.) Furthermore, you must be consistent about using super everywhere. Skip it once (as in the above example) and you defeat the entire purpose of super. See the link for more pitfalls.

If you have full control over your class hierarchy, or you avoid inheritance diamonds, then there is no need for super.


There's no penalty as-is, though your example is somewhat misguided. In the first example, it should be

super(SubClass, instance).method(args)  # Sub, not SuperClass

and that leads me to quote the Python docs:

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes that are unknown prior to runtime).

Basically, by using the first method you don't have to hard-code your parent class in there for single-class hierarchies, and you simply can't really do what you want (efficiently/effectively) using the second method when using multiple inheritance.

ReferenceURL : https://stackoverflow.com/questions/5033903/python-super-method-and-calling-alternatives

반응형