python super() - two argument version in context of __new__
我已经多次阅读了
Return a proxy object that delegates method calls to a parent or
sibling class of type. This is useful for accessing inherited methods
that have been overridden in a class.
号
If the second argument is an object,
isinstance(obj, type) must be
true. If the second argument is a type,issubclass(type2, type) must
be true (this is useful for classmethods).
号
在
Typical implementations create a new instance of the class by invoking
the superclass’s __new__() method usingsuper(currentclass, with appropriate arguments and then modifying
cls).__new__(cls[, ...])
the newly-created instance as necessary before returning it.
号
埃多克斯1〔3〕
如果
"
也许这将有助于显示
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 32 33 34 35 36 37 38 | class super(object): def __init__(self, klass, object_or_klass): # The real super can be called with 0 arguments on Python 3, # but digging into the magic that makes that work isn't relevant here. if isinstance(object_or_klass, klass): mro = type(object_or_klass).__mro__ self.obj_type = type(object_or_klass) self.obj = object_or_klass elif issubclass(object_or_klass, klass): mro = object_or_klass.__mro__ self.obj_type = object_or_klass self.obj = None else: raise TypeError # Set up a copy of the MRO to search, # with everything up to and including klass skipped self.searchlist = mro[mro.index(klass)+1:] def __getattribute__(self, name): # self.searchlist would be infinite recursion, as would super().__getattribute__ searchlist = object.__getattribute__(self, 'searchlist') # Search the method resolution order for the attribute we want. for klass in searchlist: if name in klass.__dict__: attr = klass.__dict__[name] break else: raise AttributeError if hasattr(attr, '__get__'): # Handle descriptors. obj = object.__getattribute__(self, 'obj') obj_type = object.__getattribute__(self, 'obj_type') attr = attr.__get__(obj, obj_type) return attr |
。
现在可以看到,
你一定要看雷蒙德·赫廷格在2015年的Pycon演讲,超级被认为是超级的!
但是如果没有,为什么不添加一堆打印声明来回答你的问题呢?
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # (object) is only required in Python2 class Mom(object): def __init__(self, *args, **kwargs): print('Mom is initializing - args: {!r} kwargs: {!r}'.format( args, kwargs)) def do_something(self): print('Doing some Mom thing') class Dad(object): def __init__(self, *args, **kwargs): print('Dad is initializing - args: {!r} kwargs: {!r}'.format( args, kwargs)) def do_something(self): print('Doing some Dad thing') class Sister(Mom, Dad): def __init__(self, name): print('initializing a Sister with name: {!r}'.format(name)) parent = super(Sister, self) print(type(parent)) print(parent) print('Calling super __init__') parent.__init__(name) def do_something(self, value): if value == 5: print('calling method on super') super(Sister, self).do_something() else: print('Sister did something') class Brother(Mom): def __init__(self, name): print('initializing a Brother with name: {!r}'.format(name)) parent = super(Brother, self) print(type(parent)) print(parent) print('Calling super __init__') parent.__init__(name) def do_something(self, value): if value == 5: print('calling method on super') super(Brother, self).do_something() else: print('Brother did something') b = Brother('Bear') s = Sister('Moon') b.do_something(3) b.do_something(5) s.do_something(3) s.do_something(5) |
产生以下输出(添加注释):
1 2 3 4 | <type 'super'> <super: <class 'Brother'>, <Brother object>> Calling super __init__ Mom is initializing - args: ('Bear',) kwargs: {} |
号
显然,
1 2 3 4 5 | initializing a Sister with name: 'Moon' <type 'super'> <super: <class 'Sister'>, <Sister object>> Calling super __init__ Mom is initializing - args: ('Moon',) kwargs: {} |
您会注意到,这里没有调用dad的init函数。这是因为,如果你看雷蒙德的演讲,你会知道
1 2 3 | Brother did something calling method on super Doing some Mom thing |
。
你看到同样的行为在这里重复
1 2 3 | Sister did something calling method on super Doing some Mom thing |
如果您将Sister的订单更改为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | initializing a Brother with name: 'Bear' <type 'super'> <super: <class 'Brother'>, <Brother object>> Calling super __init__ Mom is initializing - args: ('Bear',) kwargs: {} initializing a Sister with name: 'Moon' <type 'super'> <super: <class 'Sister'>, <Sister object>> Calling super __init__ Dad is initializing - args: ('Moon',) kwargs: {} Brother did something calling method on super Doing some Mom thing Sister did something calling method on super Doing some Dad thing |
。
总结如下:
代理是代表其他事物的东西。在我们的例子中,
老实说,我找不到任何有意义的兄弟姐妹工作。我不知道你什么时候会需要它。
返回
只有当你有从
打印(IsInstance(Object,Object))打印(IsInstance(Type,Type))打印(IsInstance(Object,Type))打印(IsInstance(Type,Object))
在这个过程中有一些相当复杂的魔法。