Message165983
| Author |
davide.rizzo |
| Recipients |
Arfrever, amaury.forgeotdarc, davide.rizzo, docs@python |
| Date |
2012年07月20日.22:37:52 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1342823873.36.0.576466728664.issue15360@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
Amaury, I don't honestly know, I would have proposed something otherwise.
I have been advised on #python (Freenode) not to assign to obj.__dict__ because its behaviour changes between versions and implementations, but I wouldn't know what has changed between CPython version.
One obscure thing is what can be assigned to __dict__. For class dicts no assignment is allowed. Object dicts can be assigned dict objects. If you try to assign a non-dict mapping it will complain.
However you can assign a dict-derived object, something like:
class SillyDict(dict):
def __getitem__(self, key):
return "hello"
obj.__dict__ = SillyDict()
Unfortunately accessing the attributes of obj will still use PyDict_* functions, thus ignoring any magic methods.
PyPy's behavior is slightly different. A class __dict__ attribute still is read only, but you can assign any mapping object to an object __dict__. |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2012年07月20日 22:37:53 | davide.rizzo | set | recipients:
+ davide.rizzo, amaury.forgeotdarc, Arfrever, docs@python |
| 2012年07月20日 22:37:53 | davide.rizzo | set | messageid: <1342823873.36.0.576466728664.issue15360@psf.upfronthosting.co.za> |
| 2012年07月20日 22:37:52 | davide.rizzo | link | issue15360 messages |
| 2012年07月20日 22:37:52 | davide.rizzo | create |
|