그냥 설명자 패턴을 사용하고 싶었지만 잘 작동하지 않았습니다. 다음은 간단한 예제 (단지 보여주기 위해 실제 사용하지 않고)입니다 :비단뱀 __set__ 작동
class Num(object):
def__init__(self, val=0):
self.val = val
def __get__(self, instance, owner):
return self.val
def __set__(self, instance, val):
self.val = val
def __str__(self):
return "Num(%s)" % self.val
def __repr__(self):
return self.__str__()
class Test(object):
def __init__(self, num=Num()):
self.num = num
및 테스트 :
>>>t = Test()
>>>t.num # OK
Num(0)
>>>t.num + 3 #OK i know how to fix that, but I thought __get__.(t.num, t, Test) will be called
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Num' and 'int'
>>> t.num = 4 # why isn't __set__(t.num, t, 4) called here?
>>> t.num
4
내 오해가 여기에 무엇입니까?
Thx, 클래스 속성이있는 힌트입니다. 방금 그 세부 사항을 놓쳤습니다. repr (t.num)이 예상대로 작동하지 않는다면, 적어도 t.num + 3은 예상대로 작동 할 것이고, 이것은 내가 실제 사용하는 경우에 원했던 것입니다 –