在写程序时,经常会碰见传递过多参数的情况。先看一个例子

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

def func1(id, name, age, **kw):
amount = 1
duration = 10
func2(id = id, name = name, age = age, amount = amount, **kw)


def func2(**kw):
print(kw)

func1()func2() 传递参数,一个一个的写真的很痛苦,写一次还好,关键是到处都有这种情况。

可不可以将想要的参数打包直接传递过去呢?

内置方法 locals() 就可以达到这个效果,它可以实时收集当前作用域的参数,并返回一个字典。

先在全局范围内看看有哪些参数

1
2
3
4
5
6
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

if __name__ == "__main__":
print(locals())
1
2
$ python locals_demo.py
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x102c75860>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'locals_demo.py', '__cached__': None}

可能你到没注意到一个空文件里都有这么多的内置参数。

locals() 收集参数是实时进行的,比如我们定义一个参数

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])


if __name__ == "__main__":
print(locals())
name = 'wxnacy'
print(locals())
1
2
3
4
$ python locals_demo.py

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x1083d5860>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'locals_demo.py', '__cached__': None}
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x1083d5860>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'locals_demo.py', '__cached__': None, 'name': 'wxnacy'}

第二次打印的结果中,就多出了刚刚定义的参数 name

在方法中使用也是如此

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

def func(**kw):
name = 'wxnacy'
print(locals())

if __name__ == "__main__":
func(url = 'https://wxnacy.com')
1
2
$ python locals_demo.py
{'kw': {'url': 'https://wxnacy.com'}, 'name': 'wxnacy'}

因为 locals() 方法的值是动态变的,所以我们可以先用变量储存起来,并且记得去掉不需要的参数

1
2
3
4
5
6
7
8
9
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

def func(id, age, **kw):
name = 'wxnacy'
args = locals()
args.pop('name')
print(args)

或者

1
2
3
4
5
6
7
8
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

def func(id, age, **kw):
args = locals()
name = 'wxnacy'
print(args)

现在再来回顾下开始的问题,这下解决方式简单了很多

1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])

def func1(id, name, age, **kw):
amount = 1
args = locals()
duration = 10
func2(**args)

def func2(**kw):
print(kw)

喔噢,我爱 Python。

03-16 16:20