【发布时间】:2017-02-23 16:09:37
【问题描述】:
我在一个类中有两个函数,plot() 和 show()。 show(),作为一种方便的方法,除了在plot()的代码中添加两行类似
def plot(
self,
show_this=True,
show_that=True,
color='k',
boundary_color=None,
other_color=[0.8, 0.8, 0.8],
show_axes=True
):
# lots of code
return
def show(
self,
show_this=True,
show_that=True,
color='k',
boundary_color=None,
other_color=[0.8, 0.8, 0.8],
show_axes=True
):
from matplotlib import pyplot as plt
self.plot(
show_this=show_this,
show_that=show_that,
color=color,
boundary_color=boundary_color,
other_color=other_color,
show_axes=show_axes
)
plt.show()
return
这一切都有效。
我遇到的问题是,show() 包装器中的代码似乎方式太多。我真正想要的:让show() 具有与plot() 相同的签名和默认参数,并将所有参数转发给它。
有什么提示吗?
【问题讨论】:
-
inspect模块从 Python 3.3 开始就有一个Signature类。 -
Python 2 还是 Python 3?后者复制签名的可能性更大
-
代码必须与 Python 2 兼容,但我很好奇你提到的可能性。