inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

우리를 위한 프로그래밍 : 파이썬 중급 (Inflearn Original)

매직 메소드(2-2)

p_chapter03_02.py 에서 __max__를 추가해봤는데요

308

임주아

작성한 질문수 3

0

자꾸 오류가 나는데.. 이유가 뭘까요..

아래를 Vector안에 넣고 

--------

    def __max__(self, other):

        '''__max__'''

        return Vector(max(self._x, other._x), max(self._y, other._y))

-----

아래와 같이 실행했을때요.. 

v1=Vector(1,5)

v2=Vector(3,4)

print(max(v1, v2))

django python

답변 3

0

최준만

제가 많은 도움이 될지는 모르겠지만, __max__가 매직메소드가 아닌 것과 연관이 있는거 같네요

출력문을 print(Vector.__max__(v1,v2)) 로 클래스로 호출시에만 결과값이 출력되네요.

0

임주아

다른 예제는 모두 실행됐는데

max 한번 해보자 해서 해보니 안돼서요..

class Vector(object):

    def __init__(self, *args):

        '''

        Create a Vector, example: v=Vector(5,10)

        '''

        if(len(args))==0:

            self._x, self._y=0,0

        else:

            self._x, self._y=args

    def __repr__(self):

        '''Return the vector informations.'''

        return 'Vector(%r, %r)' % (self._x, self._y)

    def __add__(self, other):

        return Vector(self._x + other._x, self._y + other._y)

    def __mul__(self, other):

        return Vector(self._x*other._x, self._y*other._y)

    def __bool__(self):

        return bool(max(self._x, self._x))

    def __max__(self, other):

        '''__max__'''

        return Vector(max(self._x, other._x), max(self._y, other._y))

v1=Vector(1,5)

v2=Vector(3,4)

print(max(v1, v2))

---실행시 오류문

TypeError: '>' not supported between instances of 'Vector' and 'Vector'

0

좋은사람

안녕하세요. 주아님

올려주신 코드상에는 특별히 에러내용은 보이지 않는것같은데

__init__ 구현 부분을 살펴보시야 될 것 같아요.

진도미확인 (진도가 안 넘어감)

0

41

1

RuntimeError: There is no current event loop in thread 'MainThread'

0

84

1

chapter 07-01 실행 오류

0

67

1

Chapter05-04 : sum_func의 합이 이중 출력됩니다.

0

78

2

45. 병렬성 2 - Futures(5-1) wait가 의도된 대로 동작하지 않습니다.

0

93

2

멀티스레딩 - 44. 병렬성 1 - Futures(4-2)

0

71

2

chapter05_02 클로저 질문사항 !

0

53

1

LV1 설정

0

88

2

매직 메소드의 호출 방법 차이 문의

0

133

2

왜 numbers리스트를 만들때 str으로 숫자를 감싸나요?

0

96

2

스크랩핑 실습 중 Mac OS 인증서 문제

0

129

1

AsyncIO 멀티 스크랩핑 실습 예제 관련 질문

0

168

1

강의자료가 영상과 다릅니다

0

256

2

closure.cell_contents 초기화 하기 문의

0

131

2

map함수 사용 시, list변환 방법 문의

0

337

2

__mul__ 백터 * 숫지, 백터 * 백터 처리

0

144

1

del처리후 질문입니다.

0

154

1

car_list(car1,car2,car3) 인스턴스 tuple 타입 문의

0

224

1

coroutine에 대한 질문

0

243

1

제너레이터 이터레이터 질문이 있습니다.

0

250

1

atom install package search not working

0

277

1

Magic Method - Not Implemented

0

401

1

가상환경 질문!

0

405

1

병렬처리 추가 질문이 있습니다.

0

246

1