我想创建装饰器来显示哪些参数被传递给函数和方法。我已经编写了函数的代码,但是方法让我很头疼。
这是按预期工作的函数装饰器:
from functools import update_wrapper
class _PrintingArguments:
def __init__(self, function, default_comment, comment_variable):
self.function = function
self.comment_variable = comment_variable
self.default_comment = default_comment
update_wrapper(wrapped=function, wrapper=self)
def __call__(self, *args, **kwargs):
comment = kwargs.pop(self.comment_variable, self.default_comment)
params_str = [repr(arg) for arg in args] + ["{}={}".format(k, repr(v)) for k, v in kwargs.items()]
function_call_log = "{}({})".format(self.function.__name__, ", ".join(params_str))
print("Function execution - '{}'\n\t{}".format(comment, function_call_log))
function_return = self.function(*args, **kwargs)
print("\tFunction executed\n")
return function_return
def function_log(_function=None, default_comment="No comment.", comment_variable="comment"):
if _function is None:
def decorator(func):
return _PrintingArguments(function=func, default_comment=default_comment, comment_variable=comment_variable)
return decorator
else:
return _PrintingArguments(function=_function, default_comment=default_comment, comment_variable=comment_variable)
# example use:
@function_log
def a(*args, **kwargs):
pass
@function_log(default_comment="Hello World!", comment_variable="comment2")
def b(*args, **kwargs):
pass
a(0, x=1, y=2)
a(0, x=1, y=2, comment="Custom comment!")
b("a", "b", "c", asd="something")
b("a", "b", "c", asd="something", comment2="Custom comment for b!")
代码执行的输出:
Function execution - 'No comment.'
a(0, y=2, x=1)
Function executed
Function execution - 'Custom comment!'
a(0, y=2, x=1)
Function executed
Function execution - 'Hello World!'
b('a', 'b', 'c', asd='something')
Function executed
Function execution - 'Custom comment for b!'
b('a', 'b', 'c', asd='something')
Function executed
我已经为方法尝试了完全相同的装饰器:
class A:
def __init__(self):
pass
@function_log
def method1(self, *args, **kwargs):
print("\tself = {}".format(self))
@function_log(default_comment="Something", comment_variable="comment2")
def method2(self, *args, **kwargs):
print("\tself = {}".format(self))
a_obj = A()
a_obj.method1(0, 1, p1="abc", p2="xyz")
a_obj.method1(0, 1, p1="abc", p2="xyz", comment="My comment")
a_obj.method2("a", "b", p1="abc", p2="xyz")
a_obj.method2("a", "b", p1="abc", p2="xyz", comment="My comment 2")
输出是:
Function execution - 'No comment.'
method1(0, 1, p2='xyz', p1='abc')
self = 0
Function executed
Function execution - 'My comment'
method1(0, 1, p2='xyz', p1='abc')
self = 0
Function executed
Function execution - 'Something'
method2('a', 'b', p2='xyz', p1='abc')
self = a
Function executed
Function execution - 'Something'
method2('a', 'b', comment='My comment 2', p2='xyz', p1='abc')
self = a
Function executed
我的装饰器没有将参数“self”传递给该方法。
我想编写第二个装饰器“method_log”,它的工作原理与“function_log”非常相似。对于代码:
class A:
def __init__(self):
pass
@method_log
def method1(self, *args, **kwargs):
print("\tself = {}".format(self))
@fmethod_log(default_comment="Something", comment_variable="comment2")
def method2(self, *args, **kwargs):
print("\tself = {}".format(self))
a_obj = A()
a_obj.method1(0, 1, p1="abc", p2="xyz")
a_obj.method1(0, 1, p1="abc", p2="xyz", comment="My comment")
a_obj.method2("a", "b", p1="abc", p2="xyz")
a_obj.method2("a", "b", p1="abc", p2="xyz", comment="My comment 2")
我想要输出:
Method execution - 'No comment.'
method1(<__main__.A instance at ...>, 0, 1, p2='xyz', p1='abc')
self = <__main__.A instance at ...> #
Function executed
Method execution - 'My comment'
method1(<__main__.A instance at ...>, 0, 1, p2='xyz', p1='abc')
self = <__main__.A instance at ...>
Function executed
Method execution - 'Something'
method2(<__main__.A instance at ...>, 'a', 'b', p2='xyz', p1='abc')
self = <__main__.A instance at ...>
Function executed
Method execution - 'Something'
method2(<__main__.A instance at ...>, 'a', 'b', comment='My comment 2', p2='xyz', p1='abc')
self = <__main__.A instance at ...>
Function executed