クラス内に2つの関数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()
のコードに2行を追加するよりも、他には何もしません。
この問題は、の方法は、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'クラスを持っています。 –
Python 2またはPython 3?後者は署名をコピーする可能性がさらに高い –
コードはPython 2と互換性がなければなりませんが、あなたが言及している可能性については興味があります。 –