使用Python中的默认值与输入参数混合

Kev*_*n91 3 python default function default-value

我有这样的功能:

def CreateURL(port=8082,ip_addr ='localhost',request='sth'):
    return str("http://" + ip_addr+":"+str(port) + '/' + request)
Run Code Online (Sandbox Code Playgroud)

现在我想使用默认参数port,request但不是ip_addr.在这种情况下如何编写函数?

CreateURL('192.168.2.1')
Run Code Online (Sandbox Code Playgroud)

不起作用,因为它会覆盖port而不是ip_addr

tim*_*geb 6

明确传递参数.

>>> def foo(a=1, b=2, c=3):
...     print(a, b, c)
... 
>>> foo(c=4)
(1, 2, 4)
Run Code Online (Sandbox Code Playgroud)