는 또한 다른 종류의 작업을 수행하도록 설계 mint
클래스 인스턴스가 오른쪽에있을 때 클래스가 추가 작업을 제어하도록하려면 어떻게해야합니까?
class mint(object):
def __init__(self, i):
self.i = i
def __add__(self, other):
o = other.i if isinstance(other, mint) else other
return mint(1 + self.i + o)
def __repr__(self):
return str(self.i)
내 수업 고려하십시오.
a = mint(1)
a + 1 + 2
6
그러나 내 개체가 오른쪽에있는 동안 추가하는 기능이 작동하지 않습니다.
1 + a
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-519-8da568c0620a> in <module>() ----> 1 1 + a
TypeError: unsupported operand type(s) for +: 'int' and 'mint'
질문 : 어떻게 1 + a
이 작동하도록 내 클래스를 수정합니까?
http://stackoverflow.com/a/5082229/6394138 – Leon
뿐만 아니라 또한 ['.__ iadd__'] 구현 고려 아래와 같이 ('.__ radd__' 구현 https://docs.python.org/3.5/reference/datamodel.html#object.__iadd__). –
'pandas '밖에는 모험심이 들었다. –