내가 가지고있는 다음과 같은 클래스 :__getattr__을 사용하여 속성에 메소드를 위임하는 방법은 무엇입니까?
이 i.__add__(1)
가 TypeError: unsupported operand type(s) for +: 'MyInt' and 'int'
를 호출하지 않습니다 :
class MyInt:
def __init__(self, v):
if type(v) != int:
raise ValueError('value must be an int')
self.v = v
def __getattr__(self, attr):
return getattr(self.v, attr)
i = MyInt(0)
print(i + 1)
나는 오류가? 그리고 그러한 메소드가 MyInt
클래스에 없으면 __getattr__
을 호출하면 안됩니까?
[정확성을 위해 인스턴스 속성을 우회하는 것 외에도, 암시 적 특수 메소드 조회는 일반적으로 객체의 메타 클래스도 '__getattribute __()'메소드를 무시합니다.] (https://docs.python.org/3/ –