On Sat, Apr 12, 2003, Guido van Rossum wrote: > > Using the dictionary doesn't work either: > > >>> str.__dict__['reverse'] = reverse > Traceback (most recent call last): > File "<stdin>", line 1, in ? > TypeError: object does not support item assignment > >>> > > But here's a trick that *does* work: > > >>> object.__setattr__(str, 'reverse', reverse) > >>> > > Proof that it worked: > > >>> "hello".reverse() > 'olleh' > >>> This post inspired me to check the way new-style class instances work with properties. Running the following code will demonstrate that although the __setattr__ hack is blocked, you can still access the instance's dict. This can obviously be fixed by using __slots__, but that seems unwieldy. Should we do anything? class C(object): def _getx(self): print "getting x:", self._x return self._x def _setx(self, value): print "setting x with:", value self._x = value x = property(_getx, _setx) a = C() a.x = 1 a.x object.__setattr__(a, 'x', 'foo') a.__dict__['x'] = 'spam' print a.__dict__['x'] -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ Why is this newsgroup different from all other newsgroups?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4