我的第一个问题,请温柔。我搜索但无法在此处或其他地方找到答案。
请注意,此问题不适用于解包 *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) 当与partial一起使用时,我在python3中遇到关键字唯一参数功能的一些困难行为.关于仅关键字参数的其他信息.
这是我的代码:
def awesome_function(a = 0, b = 0, *, prefix):
print('a ->', a)
print('b ->', b)
print('prefix ->', prefix)
return prefix + str(a+b)
Run Code Online (Sandbox Code Playgroud)
以下是我对部分的理解:
>>> two_pow = partial(pow, 2)
>>> two_pow(5)
32
>>>
Run Code Online (Sandbox Code Playgroud)
我在上面的例子中理解的是,partial使第二个参数pow作为唯一的参数two_pow.
我的问题是为什么以下工作:
>>> g = partial(awesome_function, prefix='$')
>>> g(3, 5)
a -> 3
b -> 5
prefix -> $
'$8'
>>>
Run Code Online (Sandbox Code Playgroud)
但我得到的错误是:
>>> awesome_function(prefix='$', 3, 5)
File "<stdin>", line 1
SyntaxError: non-keyword arg after …Run Code Online (Sandbox Code Playgroud) 我正在检查一些代码,发现了以下函数:
def foo(self, arg1=1, *, arg2=2):
pass
Run Code Online (Sandbox Code Playgroud)
*我很惊讶地看到关键字参数位于位置参数的左侧。然后我注意到我可以foo通过以下两种方式进行调用:
>>> foo(1)
>>> foo(arg1=1)
Run Code Online (Sandbox Code Playgroud)
我想我会期望第二次调用失败,因为我foo通过提供关键字来使用命名参数进行调用arg1。
话虽如此,我在这两种情况下都使用位置参数,还是第二次调用foo命名参数?
我使用的代码有一个类定义,该__init__方法包含一个*作为参数。举个简单的例子,
class Foo:
def __init__(self, *, bar=None):
self.bar = bar
Run Code Online (Sandbox Code Playgroud)
这是 Python 3.6 中的工作代码。什么是*争论吗?
我知道的意思和用法*args。但是有时候没什么好比args的*。例如,在函数中pprint
def pprint(object, stream=None, indent=1, width=80, depth=None, *,
compact=False):
"""Pretty-print a Python object to a stream [default is sys.stdout]."""
printer = PrettyPrinter(
stream=stream, indent=indent, width=width, depth=depth,
compact=compact)
printer.pprint(object)
Run Code Online (Sandbox Code Playgroud)
*签名中有一个。这是什么意思?
def iglob(pathname, *, recursive=False):
"""Return an iterator which yields the paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
fnmatch. However, unlike fnmatch, filenames starting with a
dot are special cases that are not matched by '*' and '?'
patterns.
If recursive is true, the pattern '**' will match any files and
zero or more directories and subdirectories.
"""
it = _iglob(pathname, recursive)
if recursive and _isrecursive(pathname):
s = next(it) # skip empty string
assert …Run Code Online (Sandbox Code Playgroud) 参考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参数?也许我理解错了......
我在看glob函数的定义,我注意到第二个参数很简单*.
def glob(pathname, *, recursive=False):
"""Return a list of paths matching a pathname pattern.
[...]
"""
return list(iglob(pathname, recursive=recursive))
Run Code Online (Sandbox Code Playgroud)
有什么意义*?
我有一个简单的程序,它应该按键对数组进行排序。
为什么sorted()函数说它只需要 1 个参数,而我没有提供任何参数?
import operator
array = [[1, 6, 3], [4, 5, 6]]
sorted_array = sorted(iterable=array, key=operator.itemgetter(array[0][1]), reverse=True)
print(sorted_array)
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
import operator
array = [[1, 6, 3], [4, 5, 6]]
sorted_array = sorted(iterable=array, key=operator.itemgetter(array[0][1]), reverse=True)
print(sorted_array)
Run Code Online (Sandbox Code Playgroud) class Field:
def __init__(self, *, read_only=False, write_only=False):
do_something()
Run Code Online (Sandbox Code Playgroud)
指定“*”有什么用以及它与使用 *args 有何不同?