• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

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

20.03.11 06:39 작성 조회수 140

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))

답변 3

·

답변을 작성해보세요.

0

최준만님의 프로필

최준만

2020.08.27

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

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

0

임주아님의 프로필

임주아

질문자

2020.03.12

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

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__ 구현 부분을 살펴보시야 될 것 같아요.