[issue16894] Function attribute access doesn't invoke methods in dict subclasses

David Beazley report at bugs.python.org
Tue Jan 8 19:43:12 CET 2013


New submission from David Beazley:

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 ;-).

----------
components: Interpreter Core
messages: 179364
nosy: dabeaz
priority: normal
severity: normal
status: open
title: Function attribute access doesn't invoke methods in dict subclasses
type: behavior
versions: Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16894>
_______________________________________


More information about the Python-bugs-list mailing list