[New-bugs-announce] [issue14658] Overwriting dict.__getattr__ is inconsistent

Albert Zeyer report at bugs.python.org
Tue Apr 24 00:53:33 CEST 2012


New submission from Albert Zeyer <albzey at googlemail.com>:

```
class Foo1(dict):
    def __getattr__(self, key): return self[key]
    def __setattr__(self, key, value): self[key] = value

class Foo2(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

o1 = Foo1()
o1.x = 42
print(o1, o1.x)

o2 = Foo2()
o2.x = 42
print(o2, o2.x)
```

With CPython 2.5, 2.6 (similarly in 3.2), I get:
({'x': 42}, 42)
({}, 42)

With PyPy 1.5.0, I get the expected output::
({'x': 42}, 42)
({'x': 42}, 42)

I asked this also on SO: http://stackoverflow.com/questions/6305267/python-inconsistence-in-the-way-you-define-the-function-setattr

>From the answers, I am not exactly sure wether this is considered as a bug in CPython or not. Anyway, I just wanted to post this here.

----------
components: None
messages: 159099
nosy: Albert.Zeyer
priority: normal
severity: normal
status: open
title: Overwriting dict.__getattr__ is inconsistent
type: behavior
versions: Python 2.7

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


More information about the New-bugs-announce mailing list