메소드(Method) |
연산자(Operator) |
인스턴스 o에 대한 사용 예 |
\_\_add\_\_(self, B) |
+ (이항) |
o + B, o += B |
\_\_sub\_\_(self, B) |
- (이항) |
o - B, o -= B |
\_\_mul\_\_(self, B) |
\* |
o \* B, o \*= B |
\_\_div\_\_(self, B) |
/ |
o / B, o /= B |
\_\_floordiv\_\_(self, B) |
// |
o // B, o //= B |
\_\_mod\_\_(self, B) |
% |
o % B, o %= B |
\_\_divmod\_\_(self, B) |
divmod() |
divmod(o, B) |
\_\_pow\_\_(self, B) |
pow(), \*\* |
pow(o, B), o \*\* B |
\_\_lshift\_\_(self, B) |
<< |
o << B, o <<= B |
\_\_rshift\_\_(self, B) |
>> |
o >> B, o >>= B |
\_\_and\_\_(self, B) |
& |
o & B, o &= B |
\_\_xor\_\_(self, B) |
^ |
o ^ B, o ^= B |
\_\_or\_\_(self, B) |
| |
o | B, o |= B |
\_\_neg\_\_(self) |
- (단항) |
-A |
\_\_abs\_\_(self) |
abs() |
abs(o) |
\_\_pos\_\_(self) |
+ (단항) |
+o |
\_\_invert\_\_(self) |
~ |
~o |
class Integer:
def __init__(self, i):
self.i = i
def __str__(self):
return str(self.i)
def __add__(self, other):
return self.i + other
i = Integer(10)
print i
print str(i)
print
i = i + 10
print i
print
i += 10
print i
10 10 20 30
class MyString:
def __init__(self, str):
self.str = str
def __div__(self, sep): # 나누기 연산자 /가 사용되었을 때 호출되는 함수
return self.str.split(sep) # 문자열 self.str을 sep를 기준으로 분리
m = MyString("abcd_abcd_abcd")
print m / "_"
print m / "_a"
print
print m.__div__("_")
['abcd', 'abcd', 'abcd'] ['abcd', 'bcd', 'bcd'] ['abcd', 'abcd', 'abcd']
class MyString:
def __init__(self, str):
self.str = str
def __div__(self, sep):
return str.split(self.str, sep)
__rdiv__ = __div__
m = MyString("abcd_abcd_abcd")
print m / "_"
print m / "_a"
print
print "_" / m
print "_a" / m
['abcd', 'abcd', 'abcd'] ['abcd', 'bcd', 'bcd'] ['abcd', 'abcd', 'abcd'] ['abcd', 'bcd', 'bcd']
class MyString:
def __init__(self, str):
self.str = str
def __div__(self, sep):
return str.split(self.str, sep)
__rdiv__ = __div__
def __neg__(self):
t = list(self.str)
t.reverse()
return ''.join(t)
__invert__ = __neg__
m = MyString("abcdef")
print -m
print ~m
fedcba fedcba
메소드 | 연산자 | 비고 |
---|---|---|
__cmp__(self, other) | 아래 메소드가 부재한 상황에 호출되는 메소드 | |
__lt__(self, other) | self < other | |
__le__(self, other) | self <= other | |
__eq__(self, other) | self == other | |
__ne__(self, other) | self != other | |
__gt__(self, other) | self > other | |
__ge__(self, other) | self >= other |
class MyCmp:
def __cmp__(self, y):
return 1 - y
c = MyCmp()
print c > 1 # c.__cmp__(1)을 호출, 반환값이 양수이어야 True
print c < 1 # c.__cmp__(1)을 호출, 반환값이 음수이어야 True
print c == 1 # c.__cmp__(1)을 호출, 반환값이 0이어야 True
False False True
class MyCmp2:
def __lt__(self, y):
return 1 < y
m = MyCmp2()
print m < 10 # m.__lt__(10)을 호출
print m < 2
print m < 1
True True False
class MyCmp3:
def __eq__(self, y):
return 1 == y
m = MyCmp3()
print m == 10 # m.__eq__(10)을 호출
m1 = MyCmp3()
print m == 1
class MyCmp4:
def __init__(self, value):
self.value = value
def __cmp__(self, other):
if self.value == other:
return 0
m2 = MyCmp4(10)
print m2 == 10
False True True
메소드 | 연산자 |
---|---|
__len__(self) | len() |
__contains__(self, item) | item in self |
__getItem__(self, key) | self[key] |
__setItem__(self, key, value) | self[key] = value |
__delItem__(self, key) | del self(key) |
class Square:
def __init__(self, end):
self.end = end
def __len__(self):
return self.end
def __getitem__(self, k):
if k < 0 or self.end <= k:
raise IndexError, k
return k * k
s1 = Square(10)
print len(s1) # s1.__len__()
print s1[1] #s1.__getitem__(1)
print s1[4]
print s1[20]
10 1 16
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-8-78c6c0117c4f> in <module>() 13 print s1[1] #s1.__getitem__(1) 14 print s1[4] ---> 15 print s1[20] <ipython-input-8-78c6c0117c4f> in __getitem__(self, k) 6 def __getitem__(self, k): 7 if k < 0 or self.end <= k: ----> 8 raise IndexError, k 9 return k * k 10 IndexError: 20
for x in s1:
print x,
0 1 4 9 16 25 36 49 64 81
print list(s1)
print tuple(s1)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)
class MyDict:
def __init__(self, d = None):
if d == None: d = {}
self.d = d
def __getitem__(self, k): #key
return self.d[k]
def __setitem__(self, k, v):
self.d[k] = v
def __len__(self):
return len(self.d)
m = MyDict() #__init__호출
m['day'] = 'light' #__setitem__호출
m['night'] = 'darkness' #__setitem__호출
print m
print m['day'] #__getitem__호출
print m['night'] #__getitem__호출
print len(m) #__len__호출
<__main__.MyDict instance at 0x103cc7ab8> light darkness 2
class MyDict:
def __init__(self, d=None):
if d == None: d = {}
self.d = d
def __getitem__(self, k):
return self.d[k]
def __setitem__(self, k, v):
self.d[k] = v
def __len__(self):
return len(self.d)
def keys(self):
return self.d.keys()
def values(self):
return self.d.values()
def items(self):
return self.d.items()
m = MyDict({'one':1, 'two':2, 'three':3})
print m.keys()
print m.values()
print m.items()
['three', 'two', 'one'] [3, 2, 1] [('three', 3), ('two', 2), ('one', 1)]
class StringRepr:
def __repr__(self):
return 'repr called'
def __str__(self):
return 'str called'
s = StringRepr()
print s
print str(s)
print repr(s)
print `s`
str called str called repr called repr called
class StringRepr:
def __repr__(self):
return 'repr called'
s = StringRepr()
print s
print repr(s)
print str(s)
print `s`
repr called repr called repr called repr called
class StringRepr:
def __str__(self):
return 'str called'
s = StringRepr()
print s
print repr(s)
print str(s)
print `s`
str called <__main__.StringRepr instance at 0x103cc7f80> str called <__main__.StringRepr instance at 0x103cc7f80>
class Accumulator:
def __init__(self):
self.sum = 0
def __call__(self, *args):
self.sum += sum(args)
return self.sum
acc = Accumulator()
print acc(1,2,3,4,5)
print acc(6)
print acc(7,8,9)
print acc.sum
15 21 45 45
class Accumulator:
def __init__(self):
self.sum = 0
def __call__(self, *args):
self.sum = reduce(lambda x, y: x + y, args)
return self.sum
acc = Accumulator()
print acc(1,2,3,4,5)
print acc(6)
print acc(7,8,9)
print acc.sum
15 6 24 24
def check(obj):
if callable(obj):
print 'callable'
else:
print 'not callable'
class B:
def func(self, v):
return v
class A:
def __call__(self, v):
return v
a = A()
b = B()
check(a)
check(b)
print
print callable(a)
print callable(b)
callable not callable True False
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일