Python3函数参数中的星号

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

内容简介:最近在看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函数参数中的星号》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Elements of Information Theory

Elements of Information Theory

Thomas M. Cover、Joy A. Thomas / Wiley-Blackwell / 2006-7 / GBP 76.50

The latest edition of this classic is updated with new problem sets and material The Second Edition of this fundamental textbook maintains the book's tradition of clear, thought-provoking instr......一起来看看 《Elements of Information Theory》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具