mymethod는 데코레이터가 호출 될 때 아직 방법이 아닌 것 같습니다.방법을 꾸미는 방법을 알고있는 방법
import inspect
class decorator(object):
def __call__(self, call):
if inspect.ismethod(call): #Not working yet
obj = "method"
args = inspect.getargspec(call)[0][1:]
elif inspect.isfunction(call):
obj = "function"
args = inspect.getargspec(call)[0]
elif inspect.isclass(call):
obj = "class"
args = inspect.getargspec(call.__init__)[0][1:]
args="(%s)" % repr(args)[1:-1].replace("'","")
print "Decorate %s %s%s" % (obj, call.__name__, args)
return call
@decorator()
def myfunction (a,b): pass
@decorator()
class myclass():
def __init__(self, a, b): pass
@decorator()
def mymethod(self, a, b): pass
if inspect.isfunction(myclass.mymethod):
print "mymethod is a function"
if inspect.ismethod(myclass.mymethod):
print "mymethod is a method"
출력 :
이Decorate function myfunction(a, b)
Decorate function mymethod(self, a, b)
Decorate class myclass(a, b)
mymethod is a method
내가 첫 번째 인수가 '자기'가 있는지 알고,하지만 덜 더러운 해결책이 될 것인가?
편집 : 왜?
함수 또는 클래스 인 경우 callable 및 해당 인수 목록을 채우고 예상되는 인수를 전달할 수 있습니다. 그런 다음이를 호출하지만 메서드 인 경우에는 " 자기 "인수를 전달합니다. 다음과 같이 입력하십시오.
import inspect
class ToDo(object):
calls=[]
def do(self, **kwargs):
for call in self.calls:
if 'self' in call.args:
print "This will fail."
args = {}
for arg in call.args:
args[arg]=kwargs.get(arg, None)
call.call(**args)
TODO = ToDo()
class decorator(object):
def __call__(self, call):
if inspect.isfunction(call):
args = inspect.getargspec(call)[0]
elif inspect.isclass(call):
args = inspect.getargspec(call.__init__)[0][1:]
self.call = call
self.args = args
TODO.calls.append(self)
return call
TODO.do(a=1, b=2)
왜 당신이 시작하기 위해 알아야합니까? 대부분의 경우, 메소드는 단지 다른 호출 가능한 메소드이며 메소드가 아닌 함수처럼 정확하게 꾸밀 수 있습니다. – delnan
동의, 왜 당신이 알 필요가 있는지 알지 못하면 이에 대한 좋은 대답이 없습니다. 일반적으로, 실제로 * 다를 필요가 있다면 어쩌면 각각의 경우마다 다른 데코레이터를 사용해야합니다. –
클래스 메서드 나 함수를 처리하는지 여부에 따라 데코레이터를 암시 적으로 변경하지 마십시오. 그것은 pythonic의 반대입니다. 당신의 장식자를 상관없이 똑같이해라. (만약 ** 당신이 정말로 **이 마술 양식을 필요로한다면 *** 무엇을하고 있는지 *** 설명하라!) – Amelia