相关疑难解决方法(0)

os.removexattr 的 Python 文档——“*”(星号)参数是什么意思?

我的第一个问题,请温柔。我搜索但无法在此处或其他地方找到答案。

请注意,此问题不适用于解包 *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 operating-system file-attributes

6
推荐指数
1
解决办法
342
查看次数

理解python3函数中的'*'"仅关键字"参数表示法

当与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)

python keyword-argument python-3.x

6
推荐指数
1
解决办法
2129
查看次数

Python 中位置函数参数的默认值?

我正在检查一些代码,发现了以下函数:

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命名参数?

python

6
推荐指数
2
解决办法
4696
查看次数

__init__ 方法中的星号参数是什么意思?

我使用的代码有一个类定义,该__init__方法包含一个*作为参数。举个简单的例子,

class Foo:
    def __init__(self, *, bar=None):
        self.bar = bar
Run Code Online (Sandbox Code Playgroud)

这是 Python 3.6 中的工作代码。什么是*争论吗?

python python-3.x

5
推荐指数
0
解决办法
347
查看次数

在函数的签名中,星号在不带标识符名称的情况下在Python中意味着什么?

我知道的意思和用法*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)

*签名中有一个。这是什么意思?

python

4
推荐指数
1
解决办法
515
查看次数

* 在python函数定义中的用途是什么?

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)

python

4
推荐指数
1
解决办法
4340
查看次数

仅关键字参数

参考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参数?也许我理解错了......

python

3
推荐指数
1
解决办法
768
查看次数

Star(*)作为python函数中的参数

我在看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)

有什么意义*

python

2
推荐指数
2
解决办法
1043
查看次数

为什么 sorted() 函数说传递 3 个参数时只需要一个参数?

我有一个简单的程序,它应该按键对数组进行排序。

为什么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)

python

2
推荐指数
2
解决办法
76
查看次数

如果没有指定变量名,则只有“*”会做什么

class Field:

    def __init__(self, *, read_only=False, write_only=False):
        do_something()
Run Code Online (Sandbox Code Playgroud)

指定“*”有什么用以及它与使用 *args 有何不同?

python python-3.x

0
推荐指数
1
解决办法
65
查看次数