看看PEP 3102,它似乎与this有某种关系。
总而言之,patch 和 opts 可以接受可变参数,但后者是接受关键字参数。关键字参数作为字典传递,其中variable positional arguments would be wrapped as tuples 。
从你的例子
def import_(ui, repo, patch1=None, *patches, **opts):
u1,repo and patch1 之后的任何位置参数都将被包装为补丁中的元组。变量位置参数之后的任何关键字参数都将通过 opts 包装为 Dictionary 对象。
另一个重要的事情是,调用者有责任确保不违反条件non-keyword arg after keyword arg。
所以违反这一点的东西会引发语法错误..
例如
喜欢的电话
import_(1,2,3,test="test")
import_(1,2,3,4,test="test")
import_(1,2,3,4,5)
import_(1,2,patch1=3,test="test")
有效,但是
import_(1,2,3,patch1=4,5)
会引发语法错误SyntaxError: non-keyword arg after keyword arg
在第一个有效的情况下import_(1,2,3,test="test")
u1 = 1, repo = 2, patch1 = 3, patches = () and opts={"test":"test"}
在第二种有效情况下import_(1,2,3,patch1=4,test="test")
u1 = 1, repo = 2, patch1 = 3 , patches = (4) and opts={"test":"test"}
第三种有效情况import_(1,2,3,4,5)
u1 = 1, repo = 2, patch1 = 3 , patches=(4,5), and opts={}
在第四个有效情况下import_(1,2,patch1=3,test="test")
u1 = 1, repo = 2, patch1 = 3 , patches=(), and opts={"test":"test"}
you can use patch1 as a keywords argument but doing so you cannot wrap any variable positional arguments within patches