复制签名,转发来自包装器函数的所有参数 [英] Copy signature, forward all arguments from wrapper function

查看:52
本文介绍了复制签名,转发来自包装器函数的所有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中有两个函数, plot() show(). show()作为方便的方法,除了在 plot()的代码中添加两行之外,没有做任何其他事情,例如

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

这一切正常.

我遇到的问题是,在 show()包装程序中,这似乎太多了.我真正想要的是:让 show() plot()具有相同的签名和默认参数,然后将所有参数转发给它.

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.

有任何提示吗?

推荐答案

您可以使用参数打包/解包:

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()

这篇关于复制签名,转发来自包装器函数的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆