2015年9月25日 星期五

[ Python 常見問題 ] Python super method and calling alternatives

Source From Here 
Question 
I see everywhere examples that super-class methods should be called by: 
  1. super(SuperClass, instance).method(args)  
Is there any disadvantage to doing: 
  1. SuperClass.method(instance, args)  
How-To 
Consider the following situation: 
  1. class A(object):  
  2.     def __init__(self):  
  3.         print('Running A.__init__')  
  4.         super(A,self).__init__()  
  5. class B(A):  
  6.     def __init__(self):  
  7.         print('Running B.__init__')          
  8.         # super(B,self).__init__()  
  9.         A.__init__(self)   
  10.   
  11. class C(A):  
  12.     def __init__(self):  
  13.         print('Running C.__init__')  
  14.         super(C,self).__init__()  
  15. class D(B,C):  
  16.     def __init__(self):  
  17.         print('Running D.__init__')  
  18.         super(D,self).__init__()  
  19.   
  20. foo=D()  
So the classes form a so-called inheritance diamond: 
  1.     A   
  2.   /   \  
  3. B      C  
  4.   \   /  
  5.     D  
Running the code yields 
Running D.__init__
Running B.__init__
Running A.__init__

That's bad because C's __init__ is skipped. The reason for that is because B's __init__ calls A's __init__ directly. The purpose of super is to resolve inheritance diamonds. If you un-comment 
  1. super(B,self).__init__()  
and comment-out 
  1. A.__init__(self)   
the code yields the more desireable result: 
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 callingA.__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.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...