Special use of args / kwargs

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:58:14

That's perfectly valid in Python 3 (demonstration); PEP 3102 introduced keyword-only arguments.

In Python 2.x you'll have to continue using kwargs.pop, or an appropriate decorator:

In Python 2,

The current Python function-calling paradigm allows arguments to be specified either by position or by keyword. An argument can be filled in either explicitly by name, or implicitly by position.

As a consequence def func1(a, *args, b="BBB", **kwargs) would be ambiguous and is therefore not allowed: in a call func1(1, 2), should args == () and b == 2, or args = (2,) and b == "BBB"?

This is why def func1(a, b="BBB", *args, **kwargs) is, on the other hand, accepted (even though it does not do what you want): b is always passed as the second argument, even in a call like func1(1, c=3, b=2) (it is equivalent to func1(1, 2, c=3)).

As ecatmur noted, Python 3 changed the way arguments are handled, which brings the flexibility you need.

def func1(a, *args, **kwargs):
    b, k = (lambda b="BBBB", **k: b, k)(**kwargs)
    print "a=%s b=%s args=%s kwargs=%s" % (a, b, args, k)

could be an alternative.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!