1. 안되는 Case
class Test:
def print(self, name):
def innerFnc(self, fncName):
return fncName == name
print("main function", name)
return innerFnc
t = Test()
f = t.print("A")
main function A
type(f)
<class 'function'>
f("A")
Traceback (most recent call last):
File "<pyshell#199>", line 1, in <module>
f("A")
TypeError: Test.print.<locals>.innerFnc() missing 1 required positional argument: 'fncName'
f(t, "A")
True
f(t, "B")
False
Class function으로 인식한다.
2. 안되는 Case
class Test:
def print(self, name):
def innerFnc(self, fncName):
return fncName == name
print("main function", name)
return innerFnc.__get__
t = Test()
f = t.print("A")
main function A
type(f)
<class 'method-wrapper'>
f("B")
<bound method Test.print.<locals>.innerFnc of 'B'>
f("B")("B")
False
f("B")("A")
True
f
<method-wrapper '__get__' of function object at 0x000002B45D215000>
3. 이렇게 해야 된다.
class Test:
def print(self, name):
def innerFnc(self, fncName):
return fncName == name
print("main function", name)
return innerFnc.__get__(name)
t = Test()
f = t.print("A")
main function A
type(f)
<class 'method'>
f("B")
False
f("A")
True
f
<bound method Test.print.<locals>.innerFnc of 'A'>
Callable instance method를 return한다.