I am trying to implement a class in which an attempt to access any attributes that do not exist in the current class or any of its ancestors will attempt to access those attributes from a member. Below is a trivial version of what I am trying to do.
class Foo:
def __init__(self, value):
self._value = value
def __getattr__(self, name):
return getattr(self._value, name)
if __name__ == '__main__':
print(Foo(5) > Foo(4)) # should do 5 > 4 (or (5).__gt__(4))
However, this raises a TypeError. Even using the operator module’s attrgetter class does the same thing. I was taking a look at the documentation regarding customizing attribute access, but I didn’t find it an easy read. How can I get around this?
If I understand you correctly, what you are doing is correct, but it still won’t work for what you’re trying to use it for. The reason is that implicit magic-method lookup does not use
__getattr__(or__getattribute__or any other such thing). The methods have to actually explicitly be there with their magic names. Your approach will work for normal attributes, but not magic methods. (Note that if you doFoo(5).__lt__(4)explicitly, it will work; it’s only the implicit “magic” lookup — e.g., calling__lt__when<is used) — that is blocked.)This post describes an approach for autogenerating magic methods using a metaclass. If you only need certain methods, you can just define them on the class manually.