Skip to content

(송*우 강의노트 14장) 기존 클래스의 메소드 바꾸기 #144

@AXF4

Description

@AXF4

파이썬에서 모든 값은 객체라 하셨고, 저희 수업 초반에 자료형의 연산자도 내 맘대로 바꿀 수 있다고 하셨습니다.
하지만, 아래의 상황에서 제대로 작동하지 않았습니다.

class int:
    def __init__(self, val):
        self.val = val
    
    def __add__(self, other):
        return self.val + other.val + 1
    
a = int(3)
b = int(5)
print(a+b) # 9

c = 6
d = 7
print(c+d) # 기대한 것은 14이나, 13임.

기존 있는 클래스(자료형)의 연산자의 작동 방식은 어떻게 바꿀 수 있나요?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions