Pythonic
[파이썬을 파이썬답게] class의 자동 string casting
YGSEO
2021. 3. 8. 17:40
728x90
class Coord(object):
def __init__(self, x, y):
self.x, self.y = x, y
point = Coord(1, 2)
print( '({}, {})'.format(point.x, point.y) )
# 또는
def print_coord(coord):
print( '({}, {})'.format(coord.x, coord.y) )
print_coord(point)
Using __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)
728x90