■ 클래스의 __repr__ 메소드를 사용해 객체의 데이터를 출력하는 방법을 보여준다.
※ 클래스 변수나 속성이 추가될 때마다 __repr__ 메소드도 추가 수정해야 한다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Student: def __init__(self, id : int, name : str): self.id = id self.name = name def __repr__(self): return self.__class__.__qualname__ + f"(id={self.id!r}, name={self.name!r})" def __eq__(self, other): if other.__class__ is self.__class__: return (self.id, self.name) == (other.id, other.name) return NotImplemented student1 = Student(100, "홍길동") student2 = Student(200, "이영희") print(student1) print(student2) """ Student(id=100, name='홍길동') Student(id=200, name='이영희') """ |