클래스에 두 개의 함수, plot()
및 show()
이 있습니다. show()
는 편리한 방법으로,복사 서명, 래퍼 함수의 모든 인수 전달
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
이 모든 작품처럼 plot()
의 코드에 두 줄을 추가하는 것보다 아무것도하지 않습니다.
내가 가진 문제는 방법의 코드가 show()
래퍼에 너무 많아 보이는 것입니다. 내가 정말로 원하는 바 : show()
은 동일한 서명과 기본 인수를 plot()
으로하고 모든 인수를 전달합니다.
힌트가 있습니까? 당신이 경우 지금
def show(self, *args, **kwargs):
from matplotlib import pyplot as plt
self.plot(*args, **kwargs)
plt.show()
show.__signature__ = inspect.signature(plot)
:
def show(self, *args, **kwargs):
from matplotlib import pyplot as plt
self.plot(*args, **kwargs)
plt.show()
'i nspect' 모듈은 Python 3.3부터'Signature' 클래스를 가지고 있습니다. –
파이썬 2 또는 파이썬 3? 후자는 서명을 복사 할 수있는 가능성이 더 많습니다. –
코드는 Python 2와 호환되어야하지만, 내가 언급 한 가능성에 대해 궁금 할 것입니다. –