본문 바로가기
Etc./Python

[Python] class 자동 string casting using '__string__'

by ahj 2021. 10. 11.

객체 class에 기본 string 메소드를 입력해줄 수 있는 모양이다.

__str__ 안에

class Coord(object):
    def __init__ (self, x, y):
        self.x, self.y = x, y
    def __str__ (self):
        return '({}, {})'.format(self.x, self.y)

point = Coord(1, 2)
print(point)

이런 식으로 출력 할때마다 포매팅해줄 필요 없이 그냥 프린트하면 된다

'Etc. > Python' 카테고리의 다른 글

[Python] string formatting  (0) 2022.01.03
[Python] 무한대 표현  (0) 2021.10.11
[Python] 이진 검색 함수 bisect  (0) 2021.10.11
[Python] swap  (0) 2021.10.11
[Python] for-else  (0) 2021.10.11

댓글