Im 파이썬 3.6을 사용하고 있으며 Interval 클래스를 만들고 싶습니다. 정수 및 intervall을 추가 할 수있는 add 메서드를 수행하려고합니다. 지금까지 제가 위해 I = 간격 (2,3) C = I + 1 # C 형 = [3,4]클래스와 메소드 python
그러나 반대 dosent 작업
I =주기를 작동하는 코드를 생성하는 관리 + 지원되지 않는 피연산자 타입 (들), (2,3) C = 1 + I 오류 메시지 형식 오류를 #get '내부'와 '주기'
를 folow 나의 클래스 및 방법 내 코드는클래스 간격 :
def __init__(self,start=None,end=None):
if end==None:
end=start
if start==None:
start=end
if start>end:
raise TypeError('left value must be smaller than right value')
self.start=start
self.end=end
,
데프 는 (자기, 기타)를 추가 :
if isinstance(other,int):
s2,e2=other,other
s1,e1=self.start,self.end
elif isinstance(self,int):
s1,e1=self,self
s2,e2=other.start,other.end
else:
s2,e2=other.start,other.end
s1,e1=self.start,self.end
return Interval(s1+s2,e1+e2)
나는 아무것도 작동하지 않는 경우에 추가하는 방법에 내 ELIF을 변경하려고합니다. 아무도 이것을 해결하는 방법을 알고 있습니까?
감사합니다.
고마워요! 이제 radd 메서드로 작동합니다! –
당신을 진심으로 환영합니다. –