函数参数中的星号是什么?
当我查看pickle模块时,我看到了这一点:(http://docs.python.org/3.3/library/pickle.html#pickle.dump)
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
Run Code Online (Sandbox Code Playgroud)
我知道在参数之前的单个和双星号(对于可变数量的参数),但这没有任何结果.而且我很确定这与泡菜无关.这可能就是这种情况的一个例子.我把它发送给翻译时我才知道它的名字:
>>> def func(*):
... pass
...
File "<stdin>", line 1
SyntaxError: named arguments must follow bare *
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我在python 3.3.0上.
我的第一个问题,请温柔。我搜索但无法在此处或其他地方找到答案。
请注意,此问题不适用于解包 *args 之类的参数。
在os.removexattr的 python 3.3 文档中,声明如下:
os.removexattr(path, attribute, *, follow_symlinks=True)
Removes the extended filesystem attribute attribute from path.
attribute should be bytes or str. If it is a string, it is encoded
with the filesystem encoding.
This function can support specifying a file descriptor and not
following symlinks.
Run Code Online (Sandbox Code Playgroud)
请注意,第三个参数是一个星号:*
我认为这意味着“指定一个或多个用逗号分隔的属性”,但是在尝试执行此操作时,出现异常:
import os
os.removexattr('M7-AAE-01.jpg', 'user.camera_brand', 'user.camera_model')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Function takes at most 2 positional arguments …Run Code Online (Sandbox Code Playgroud) 我对 Python 还很陌生,我想使用这个库。但是,该类的构造函数中有一个参数,我找不到任何相关信息。
init方法如下所示:
def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
* 有什么作用?据我所知,自我只是对象本身,其他只是论证。但是 * 是什么?
完整课程链接: 查看第 73 行
提前致谢
参考http://docs.python.org/3/glossary.html#term-parameter
keyword-only参数:指定只能由关键字提供的参数.可以通过在它们之前的函数定义的参数列表中包含单个var-positional参数或bare*来定义仅关键字参数,例如下面的kw_only1和kw_only2:
def func(arg, *, kw_only1, kw_only2):
Run Code Online (Sandbox Code Playgroud)
而不是单个var-location参数不应该是单个var-keyword参数?也许我理解错了......