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.

classification
Title: Customized attribute access causes infinite loop
Type: behavior Stage:
Components: Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ethan.furman, zorceta
Priority: normal Keywords:

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:18adminsetgithub: 68778
2015-07-08 19:05:19ethan.furmansettitle: Unappropriate issue -> Customized attribute access causes infinite loop
nosy: + ethan.furman

messages: + msg246466

resolution: not a bug
2015-07-08 09:17:07zorcetasettitle: Customized attribute access causes infinite loop -> Unappropriate issue
2015-07-08 09:16:31zorcetasetstatus: open -> closed
2015-07-08 09:07:22zorcetacreate