0

I don't quite understand why the code below raises a RecursionError?

class A:
    def __init__(self):
        self.x = 1


class Proxy:
    def __init__(self, obj):
        self._obj = obj

    def __getattr__(self, name):
        getattr(self._obj, name)

    def __setattr__(self, name, value):
        setattr(self._obj, name, value)


a = A()
p = Proxy(a)

I'm just trying to emulate A with Proxy.

I don't think this question is a duplication. The provided answers deal with class hierarchies which are not used here.

chhenning
  • 2,017
  • 3
  • 26
  • 44
  • 2
    `__setattr__` is called on every assignment, so `setattr` is not going to work, because it will end up calling `__setattr__` etc. Use `object.__setattr__(self._obj, name, value)`, although, check if `name == '_obj'` or else your `__init__` will fail at `self._obj = obj` – juanpa.arrivillaga Aug 13 '19 at 20:18
  • Answer can be found here: https://pastebin.com/XfXJYPyW – chhenning Aug 14 '19 at 00:03

0 Answers0