Search code examples
pythonfunctionsignature

Copy signature, forward all arguments from wrapper function


I have two functions in a class, plot() and show(). show(), as convenience method, does nothing else than to add two lines to the code of plot() like

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

This all works.

The issue I have is that this seems way too much code in the show() wrapper. What I really want: Let show() have the same signature and default arguments as plot(), and forward all arguments to it.

Any hints?


Solution

  • You can make use of argument packing/unpacking:

    def show(self, *args, **kwargs):
        from matplotlib import pyplot as plt
        self.plot(*args, **kwargs)
        plt.show()