首页 > python函数中参数带有一个星号

python函数中参数带有一个星号

def mufunc(*, x, y): # 第一个星号是什么意思?
    pass

刚刚查了一下文档,https://www.python.org/dev/peps/pep-3102/
是为了规定参数名,在这里参数只能为x和y,所以在带入实参的时候只能显示地指定名称,应该这样调用:
func(x=1,y=2)


卧槽第一次知道这种写法!

https://docs.python.org/3/reference/compound_stmts.html#function-definitions

If a parameter has a default value, all following parameters up until the “*” must also have a default value — this is a syntactic restriction that is not expressed by the grammar.


是不是函数带两个或者更多参数,但只接受最后两个

没看过这种写法啊


def mufunc(*args,x,y):
    pass

*args:元组;
**args2:词典。

【热门文章】
【热门文章】