__repr__用于返回对象的字符串表示,但在 Python 中,函数也是对象本身,并且可以具有属性。
如何设置__repr__函数的?
我在这里看到可以为函数外部的函数设置一个属性,但通常__repr__在对象定义本身内设置一个属性,所以我想在函数定义本身内设置 repr。
我的用例是,我正在使用Tenacity重试具有指数退避功能的网络函数,并且我想记录我最后调用的函数的(信息性)名称。
retry_mysql_exception_types = (InterfaceError, OperationalError, TimeoutError, ConnectionResetError)
def return_last_retry_outcome(retry_state):
"""return the result of the last call attempt"""
return retry_state.outcome.result()
def my_before_sleep(retry_state):
print("Retrying {}: attempt {} ended with: {}\n".format(retry_state.fn, retry_state.attempt_number, retry_state.outcome))
@tenacity.retry(wait=tenacity.wait_random_exponential(multiplier=1, max=1200),
stop=tenacity.stop_after_attempt(30),
retry=tenacity.retry_if_exception_type(retry_mysql_exception_types),
retry_error_callback=return_last_retry_outcome,
before_sleep=my_before_sleep)
def connect_with_retries(my_database_config):
connection = mysql.connector.connect(**my_database_config)
return connection
Run Code Online (Sandbox Code Playgroud)
目前retry_state.fn显示<function <lambda> at 0x1100f6ee0>类似于@chepner 所说的内容,但我想向其中添加更多信息。
您可以使用一个装饰器,该装饰器返回一个带有__call__和__repr__集合的类:
class CustomReprFunc:
def __init__(self, f, custom_repr):
self.f = f
self.custom_repr = custom_repr
def __call__(self, *args, **kwargs):
return self.f(*args, **kwargs)
def __repr__(self):
return self.custom_repr(self.f)
def set_repr(custom_repr):
def set_repr_decorator(f):
return CustomReprFunc(f, custom_repr)
return set_repr_decorator
@set_repr(lambda f: f.__name__)
def func(a):
return a
print(repr(func))
Run Code Online (Sandbox Code Playgroud)
我认为自定义装饰器可以帮助:
import functools
class reprable:
"""Decorates a function with a repr method.
Example:
>>> @reprable
... def foo():
... '''Does something cool.'''
... return 4
...
>>> foo()
4
>>> foo.__name__
'foo'
>>> foo.__doc__
'Does something cool.'
>>> repr(foo)
'foo: Does something cool.'
>>> type(foo)
<class '__main__.reprable'>
"""
def __init__(self, wrapped):
self._wrapped = wrapped
functools.update_wrapper(self, wrapped)
def __call__(self, *args, **kwargs):
return self._wrapped(*args, **kwargs)
def __repr__(self):
return f'{self._wrapped.__name__}: {self._wrapped.__doc__}'
Run Code Online (Sandbox Code Playgroud)
演示: http: //tpcg.io/uTbSDepz。