This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2015年07月08日 09:07 by zorceta, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Messages (2) | |||
|---|---|---|---|
| msg246453 - (view) | Author: Zorceta (zorceta) | Date: 2015年07月08日 09:07 | |
Code and result:
```
>>> class A:
def __init__(self):
self.data = {'a': 1, 'b': 2, 'c': 3}
def __getattr__(self, name):
print('in __getattr__, getting %s' % name)
if name in self.data:
return self.data[name]
else:
raise AttributeError
def __setattr__(self, name, value):
print('in __setattr__, setting %s to %s' % (name, value))
if name in self.data:
self.data[name] = value
else:
self.__class__.__setattr__(self, name, value)
>>> a = A()
in __setattr__, setting data to {'a': 1, 'b': 2, 'c': 3}
in __getattr__, getting data
in __getattr__, getting data
in __getattr__, getting data
......
```
From above you can see it stuck on
>>> if name in self.data:
in `__setattr__`.
But, the result indicates that `__setattr__` uses `__getattr__` to read the `data` attribute, which is against docs:
"Note that if the attribute is found through the normal mechanism, __getattr__() is not called."
If it acts as described, `data` should be read "through the normal mechanism", not through `__getattr__`.
|
|||
| msg246466 - (view) | Author: Ethan Furman (ethan.furman) * (Python committer) | Date: 2015年07月08日 19:05 | |
The error in the example code was in def __init__(self): self.data = ... In order to get around that issue, either `data` needs to be declared at class scope, or special cased in __getattr__. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:18 | admin | set | github: 68778 |
| 2015年07月08日 19:05:19 | ethan.furman | set | title: Unappropriate issue -> Customized attribute access causes infinite loop nosy: + ethan.furman messages: + msg246466 resolution: not a bug |
| 2015年07月08日 09:17:07 | zorceta | set | title: Customized attribute access causes infinite loop -> Unappropriate issue |
| 2015年07月08日 09:16:31 | zorceta | set | status: open -> closed |
| 2015年07月08日 09:07:22 | zorceta | create | |