将被调用函数的数据委托给另一个函数是很简单的:
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)
你不应该locals()在这里使用; 使用*argsan **kw来捕获参数并传递它们:
def test(self, *args, **kw):
self.test(*args, **kw)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |