In [5]:
class DoppelDict(dict):
def __setitem__(self, key, value):
super().__setitem__(key, [value] * 2)
In [6]:
dd = DoppelDict(one=1)
In [7]:
dd
Out[7]:
In [8]:
dd['two'] = 2
In [9]:
dd
Out[9]:
In [10]:
dd.update(three=3)
In [11]:
dd
Out[11]:
In [12]:
class AnswerDict(dict):
def __getitem__(self, key):
return 42
In [13]:
ad = AnswerDict(a='foo')
ad['a']
Out[13]:
In [14]:
d = {}
d.update(ad)
d['a']
Out[14]:
In [15]:
d
Out[15]:
In [16]:
import collections
In [17]:
class DoppelDict2(collections.UserDict):
def __setitem__(self, key, value):
super().__setitem__(key, [value] * 2)
In [18]:
dd = DoppelDict2(one=1)
dd
Out[18]:
In [19]:
dd['two'] = 2
dd
Out[19]:
In [20]:
dd.update(three=3)
dd
Out[20]:
In [21]:
class AnswerDict2(collections.UserDict):
def __getitem__(self, key):
return 42
In [22]:
ad = AnswerDict2(a='foo')
ad['a']
Out[22]:
In [23]:
d = {}
d.update(ad)
d['a']
Out[23]:
In [24]:
d
Out[24]:
In [25]:
class A:
def ping(self):
print('ping', self)
In [26]:
class B(A):
def pong(self):
print('pong:', self)
In [27]:
class C(A):
def pong(self):
print('PONG:', self)
In [39]:
class D(B, C):
def ping(self):
super().ping()
print('post-ping:', self)
def pingpong(self):
self.ping()
super().ping()
self.pong()
super().pong()
C.pong(self)
In [40]:
d = D()
d.pong()
In [41]:
C.pong(d)
In [42]:
D.__mro__
Out[42]:
In [43]:
d.ping()
In [44]:
d.pingpong()
In [45]:
bool.__mro__
Out[45]:
In [46]:
def print_mro(cls):
print(', '.join(c.__name__ for c in cls.__mro__))
In [47]:
print_mro(bool)
In [48]:
import numbers
print_mro(numbers.Integral)
In [49]:
import io
print_mro(io.BytesIO)
In [50]:
print_mro(io.TextIOWrapper)
In [51]:
import tkinter
print_mro(tkinter.Text)
In [52]:
import tkinter
In [53]:
print_mro(tkinter.Toplevel)
In [54]:
print_mro(tkinter.Widget)
In [55]:
print_mro(tkinter.Button)
In [56]:
print_mro(tkinter.Entry)
In [57]:
print_mro(tkinter.Text)
In [ ]: