2017-02-23 5 views
0

클래스에 두 개의 함수, 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() 
+0

'i nspect' 모듈은 Python 3.3부터'Signature' 클래스를 가지고 있습니다. –

+0

파이썬 2 또는 파이썬 3? 후자는 서명을 복사 할 수있는 가능성이 더 많습니다. –

+0

코드는 Python 2와 호환되어야하지만, 내가 언급 한 가능성에 대해 궁금 할 것입니다. –

답변

2

당신은 인수 포장/풀기를 사용할 수있다 IDLE과 같은 자동 완성 기능을 제공하는 셸에서 show를 사용하면 암호 대신 에 대한 올바른 매개 변수가 표시됩니다. *args, **kwargs

1

파이썬 3 실제로 검사 모듈로 포장 기능의 서명을 복사 할 수있는 기능을 제공 :