函数参数中的空星号? [英] Bare asterisk in function arguments?

查看:48
本文介绍了函数参数中的空星号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数参数中的星号有什么作用?

当我查看 pickle 模块时,我看到这个:

pickle.dump(obj, file, protocol=None, *, fix_imports=True)

我知道参数前面有一个单星号和双星号(用于可变数量的参数),但这没有放在前面.而且我很确定这与泡菜无关.这可能只是发生这种情况的一个例子.当我把这个发给翻译时,我才知道它的名字:

<预><代码>>>>定义函数(*):... 经过...文件<stdin>",第 1 行语法错误:命名参数必须跟在裸 * 之后

如果有关系,我使用的是 python 3.3.0.

解决方案

Bare * 用于强制调用者使用命名参数 - 所以你不能用 * 定义函数code> 当您没有以下关键字参数时作为参数.

请参阅此答案Python 3 文档了解更多详情.

What does a bare asterisk in the arguments of a function do?

When I looked at the pickle module, I see this:

pickle.dump(obj, file, protocol=None, *, fix_imports=True)

I know about a single and double asterisks preceding arguments (for variable number of arguments), but this precedes nothing. And I'm pretty sure this has nothing to do with pickle. That's probably just an example of this happening. I only learned its name when I sent this to the interpreter:

>>> def func(*):
...     pass
...
  File "<stdin>", line 1
SyntaxError: named arguments must follow bare *

If it matters, I'm on python 3.3.0.

解决方案

Bare * is used to force the caller to use named arguments - so you cannot define a function with * as an argument when you have no following keyword arguments.

See this answer or Python 3 documentation for more details.

这篇关于函数参数中的空星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆