+---------------+ | | | Python Object | | | +------+--------+ | ID | | +---------------+ | Type | | +---------------+ | Value| | +---------------+
def who(obj): print(id(obj), type(obj)) who(1) who(None) who(who) 4515088368 4514812344 4542646064
class A: pass a = A() who(A) who(a) 140477703944616 4542635424
class _EnumDict(dict):
def __init__(self):
dict.__init__(self)
self._member_names = []
def keys(self):
keys = dict.keys(self)
return list(filter(lambda k: k.isupper(), keys))
ed = _EnumDict()
ed['RED'] = 1
ed['red'] = 2
print(ed, ed.keys())
{'RED': 1, 'red': 2} ['RED']
print(super.__doc__) super() -> same as super(__class__, ) 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)
class A(object):
def method(self):
who(self)
print("A.method")
class B(A):
def method(self):
who(self)
print("B.method")
class C(B):
def method(self):
who(self)
print("C.method")
class D(C):
def __init__(self):
super().method()
super(__class__, self).method()
super(C, self).method() # calling C's parent's method
super(B, self).method() # calling B's parent's method
super(B, C()).method() # calling B's parent's method with instance of C
d = D()
print("\nInstance of D:")
who(d)
4542787992
C.method
4542787992
C.method
4542787992
B.method
4542787992
A.method
4542788048
A.method
Instance of D:
4542787992
super(D, d).method() # calling D's parent's method with instance d 4542787992 C.method
+----------+
| A |
+----------+
| method() <---------------+ super(B,self)
+----------+ |
|
+----------+ +----------+
| B | | D |
+----------+ super(C,self) +----------+
| method() <---------------+ method() |
+----------+ +----------+
|
+----------+ |
| C | |
+----------+ | super(D,self)
| method() <---------------+
+----------+
class A(object):
pass
class B(A):
def method(self):
print("B's method")
class C(A):
def method(self):
print("C's method")
class D(B, C):
def __init__(self):
super().method()
class E(C, B):
def __init__(self):
super().method()
d = D()
e = E()
B's method
C's method
D.mro() [__main__.D, __main__.B, __main__.C, __main__.A, object] E.mro() [__main__.E, __main__.C, __main__.B, __main__.A, object] super() 方法就是沿着 mro() 给出的顺序向上寻找起点的: super(D, d).method() super(E, e).method() B's method C's method super(C, e).method() super(B, d).method() B's method C's method
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有