Putting an example:
class B(C):
@staticmethod
def test():
print('unbound supers can only call static methods of classes')
def test2(self):
print('bound methods require bound instances or self argument')
class A(B):
def __init__(self):
self.super() # returns this object as a parent (B) instance
self.super(A, self) # returns this object as a bound B instance
self.super(B, self) # returns this object as a bound C instance
self.super(B) # returns this object as an unbound reference to B!!
self.super(B).__thisclass__.test() # usable, but complex
type(self).__mro__[1].test() # equivalent call
B.test() # when you explicitly one to point to a given method
B.test2(self) # ídem, but bound
Summarizing:
- Use self.super().method() when wanting to access the parent version of a method
- Use self.super(<type>, self).method() when wanting to go to the parent of an specific class
- Use <type>.method(self,...) when you already know the object to call
No comments:
Post a Comment