📝 스페셜 메서드
🖥️ 1. 스페셜 메서드란?
- 해당 메서드들을 구현하면 객체에 여러가지 파이썬 내장함수나 연산자에 원하는 기능을 적용 가능
class Sentence :
def __init__(self, s) :
self.words = s.split()
def __len__(self) :
return len(self.words)
s = Sentence('Somtimes bad things happlen to good people')
print(len('Hello'))
print(len([1,2,3,4,5]))
print(s.words)
print(len(s.words))
print(len(s))
[결과]
5
5
['Somtimes', 'bad', 'things', 'happlen', 'to', 'good', 'people']
7
7
class Point :
def __init__(self, x, y) :
self.x = x
self.y = y
def __str__(self) :
return('{}, {}'.format(self.x, self.y))
p1 = Point(3, 4)
print(p1)
print(str(p1))
[결과]
3, 4
3, 4