
import random
class LottoGame:
def __init__(self):
self._balls = [n for n in range(1,46)]
def pick(self):
random.shuffle(self._balls)
return sorted([random.choice(self._balls) for n in range(6)])
κ°μ²΄λ₯Ό μμ±ν΄μ€λ€. game = LottoGame()
μ€ν νμΈ
print(game.pick()) # [9, 10, 35, 38, 44, 44]
print(game()) # TypeError: 'LottoGame' object is not callable
κ°μ²΄λ₯Ό λ΄μ λ³μμ pick()λ₯Ό νΈμΆ νλ©΄ λμ§λ§ λ³μμ () λ₯Ό λΆμ¬μ νΈμΆμ νλ©΄ μλ¬κ°λλ€.
callable()ν¨μλ₯Ό μ¬μ©νλ©΄ λΆλ₯Ό μ μλμ§ μλμ§ μλ €μ€λ€.
print(callable(str), callable(list), callable(3.14), callable(game))
# True True False False
class LottoGame:
def __init__(self):
self._balls = [n for n in range(1,46)]
def pick(self):
random.shuffle(self._balls)
return sorted([random.choice(self._balls) for n in range(6)])
def __call__(self):
return self.pick()
def __call__(self):callλ©μλλ₯Ό μ€λ²λΌμ΄λ© νλ©΄ κ°λ₯νλ€ print(callable(game)) # True
print(game()) # [3, 11, 12, 14, 32, 36]
gameμ νΈμΆν΄λ κ°λ₯νλ€def args_test(name, *contents, point=None, **attrs):
return '<args_test> -> ({}) ({}) ({}) ({})'.format(name, contents, point, attrs)
1. μΈμ 1κ°λ§ 쀬μ λ
print(args_test('test1'))
# (test1) (()) (None) ({})
2. μΈμλ₯Ό 2κ° μ€¬μ λ
print(args_test('test1', 'test2'))
# (test1) (('test2',)) (None) ({})
3. μΈμ 3κ°, dict 1κ°
print(args_test('test1','test2', 'test3', id='admin'))
# (test1) (('test2', 'test3')) (None) ({'id': 'admin'})
4. + μ§μ κ°
print(args_test('test1','test2', 'test3', id='admin', point=7))
# (test1) (('test2', 'test3')) (7) ({'id': 'admin'})
μ΄μ μ₯κ³ κ°λ°ν λ ν¨μμ *args **kwargs μ μ΄ν΄νλλ‘ νμ