转发方法参数到另一个方法 - 自我需要从本地()弹出

Ter*_*non 1 python oop

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

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

def test(a, b='hoho'):
    test2(**locals())
Run Code Online (Sandbox Code Playgroud)

但是,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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

你不应该locals()在这里使用; 使用*argsan **kw来捕获参数并传递它们:

def test(self, *args, **kw):
    self.test(*args, **kw)
Run Code Online (Sandbox Code Playgroud)