如何更改方法的签名?
更准确地说,我对更改for line in c:
is_website_in_line = []
for website in blocked_sites:
is_website_in_line.append(website in line) # website in line check if a string is within another, so it returns either True or False
if not any(is_website_in_line) # if all of the list has False values
f.write(line)
方法的签名感兴趣。
请注意,这需要在对象级别而不是类级别完成,因为我想将不同的功能(具有不同的签名)传递给同一类。
我找到了与此相关的doc,但仍然找不到解决问题的方法,如下所示:
__call__
输出为:
import inspect
class A():
def __init__(self, f):
self._f = f
def __call__(self, *args, **kwargs):
return self._f(*args, **kwargs)
def sum_x_y(x, y):
"""Sums `x` and `y`."""
return x, y
a = A(sum_x_y)
print(f'I get: inspect.signature(a) = {inspect.signature(a)}')
print(f'But I would like: inspect.signature(a) = {inspect.signature(sum_x_y)}')
有什么主意吗?