Message179364
Suppose you subclass a dictionary:
class mdict(dict):
def __getitem__(self, index):
print('Getting:', index)
return super().__getitem__(index)
Now, suppose you define a function and perform these steps that reassign the function's attribute dictionary:
>>> def foo():
... pass
...
>>> foo.__dict__ = mdict()
>>> foo.x = 23
>>> foo.x # Observe: No output from overridden __getitem__
23
>>> type(foo.__dict__)
<class '__main__.mdict'>
>>> foo.__dict__
{'x': 23}
>>>
Carefully observe that access to foo.x does not invoke the overridden __getitem__() method in mdict. Instead, it just directly accesses the default __getitem__() on dict.
Admittedly, this is a really obscure corner case. However, if the __dict__ attribute of a function can be legally reassigned, it might be nice for inheritance to work ;-). |
|
Date |
User |
Action |
Args |
2013-01-08 18:43:12 | dabeaz | set | recipients:
+ dabeaz |
2013-01-08 18:43:12 | dabeaz | set | messageid: <1357670592.25.0.470525675752.issue16894@psf.upfronthosting.co.za> |
2013-01-08 18:43:12 | dabeaz | link | issue16894 messages |
2013-01-08 18:43:11 | dabeaz | create | |
|