Python3函数参数中的星号

栏目: Python · 发布时间: 7年前

内容简介:最近在看asyncio的代码,看到一个新的用法,查阅完文档之后,发现,在定义函数的参 数列表中,.. code:: python错误示范::

最近在看asyncio的代码,看到一个新的用法,查阅完文档之后,发现,在定义函数的参 数列表中, * 后的参数,调用者必须以 key=value 形式调用。因为平时看到的都 是 *args, **kwargs 形式的代码。

.. code:: python

def _make_ssl_transport(self, rawsock, protocol, sslcontext, waiter=None,
                        *, server_side=False, server_hostname=None,
                        extra=None, server=None):
    """Create SSL transport."""
    raise NotImplementedError

错误示范::

$ cat test.py
def foo(this, *, loop):
    print("<foo> been called")

foo(None, loop=None)
foo(None, None)
$ python test.py
<foo> been called
Traceback (most recent call last):
File "test.py", line 6, in <module>
    foo(None, None)
TypeError: foo() takes 1 positional argument but 2 were given

.. [#] http://stackoverflow.com/questions/14301967/python-bare-asterisk-in-function-argument

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


以上所述就是小编给大家介绍的《Python3函数参数中的星号》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Uberland

Uberland

Alex Rosenblat / University of California Press / 2018-11-19 / GBP 21.00

Silicon Valley technology is transforming the way we work, and Uber is leading the charge. An American startup that promised to deliver entrepreneurship for the masses through its technology, Uber ins......一起来看看 《Uberland》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具