class A:
def __init__(self):
print('init')
def __call__(self):
print('call')
def myfunc(self):
print('my')
print("==== OUTPUT ====")
a = A()
a()
a.myfunc()
==== OUTPUT ====
init
call
my
__init__
VS. __call__
__init__
VS. __call__
December 19, 2024
Monkey Pathλ₯Ό 곡λΆνλ€κ° νλ² μ 리νλ©΄ μ’μ κ² κ°μ Pythonμ __init__
κ³Ό __call__
μ λν΄μ μ 리νκ³ μ ν©λλ€.
__init__
μ μΈμ€ν΄μ€ μ΄κΈ°ν μ μ€ν__call__
μ μΈμ€ν΄μ€ νΈμΆ μ μ€νclass A:
def __init__(self):
print('init')
def __call__(self):
print('call')
def myfunc(self):
print('my')
print("==== OUTPUT ====")
a = A()
a()
a.myfunc()
==== OUTPUT ====
init
call
my
__init__
μ λ°μ΄ν° μμ± μ μ₯.class MyDecorator:
def __init__(self, data):
self.storage = data
def __call__(self):
print('data entered :', self.storage.__name__)
self.storage()
print('data exited :', self.storage.__name__)
@MyDecorator
def printer():
print('I print the empty space.')
print('==== start ====')
printer()
==== start ====
data entered : printer
I print the empty space.
data exited : printer