Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将方法参数转发到另一个方法-需要从locals()中弹出self_Python_Oop - Fatal编程技术网

Python 将方法参数转发到另一个方法-需要从locals()中弹出self

Python 将方法参数转发到另一个方法-需要从locals()中弹出self,python,oop,Python,Oop,将被调用函数的数据委托给另一个函数很简单: def test2(a, b): huh = locals() print huh def test(a, b='hoho'): test2(**locals()) 但是,当调用方法时,局部变量包含self,这会妨碍在一行中为方法调用执行相同操作: class X(object): def test2(self, a, b): huh = locals() print huh

将被调用函数的数据委托给另一个函数很简单:

def test2(a, b):
    huh = locals()
    print huh

def test(a, b='hoho'):
    test2(**locals())
但是,当调用方法时,局部变量包含self,这会妨碍在一行中为方法调用执行相同操作:

class X(object):
    def test2(self, a, b):
        huh = locals()
        print huh

    def test(self, a, b='hoho'):
        self.test2(**locals()) # no workie
        test2(**locals()) # no workie either
你不应该在这里使用本地人;使用*args和**kw捕获参数并传递这些参数:

def test(self, *args, **kw):
    self.test(*args, **kw)
你不应该在这里使用本地人;使用*args和**kw捕获参数并传递这些参数:

def test(self, *args, **kw):
    self.test(*args, **kw)

前一段时间,我编写了一个函数,该函数内省被调用的函数,并仅为实际存在的参数传递命名参数


但我通常支持Martijn,将局部变量传递到其他地方闻起来是个坏主意。

不久前,我编写了一个函数,该函数将对调用的函数进行内省,并仅为实际存在的参数传递命名参数


但总的来说,我支持Martijn,把当地人传给其他地方听起来是个坏主意。

这里有问题吗?你为什么要这么做?你真的有这么多的论点,你不想直接把它们传给别人吗?使用局部变量会在最微小的机会出现问题。您是否有支持局部变量是脆弱的断言的参考文献?这里有问题吗?您为什么要这样做?您真的有这么多的论点,不想直接传递它们吗?使用局部变量会在最短的时间内失效。您是否有支持局部变量是脆弱的断言的参考文献?一个缺点是,关于此类函数的帮助不会告诉您太多关于参数的信息。一个缺点是,关于此类函数的帮助不会告诉您太多关于参数的信息。